ng-repeat因对象数组而失败

时间:2016-01-26 20:36:04

标签: javascript angularjs ionic-framework angularjs-ng-repeat

我不明白为什么,没有什么不寻常的。

我有:

控制器

define([
    'underscore',
    'json!data/options.json' 
], function (_, options) {
    "use strict";

    var parseValue = function (val, type) {

        if (!type || type === "text" || type === "password") {
            return val;
        }

        switch (type) {
            case 'checkbox':
                return !!val;
            case 'number':
                return parseInt(val);
            default:
                throw new Error("Unknow type ", type);
        }
    };

    return [
        "$scope",
        "OptionsService",
        function ($scope, OptionsService) {

            $scope.ops = _.map(options, function (op, key) {
                op.id = key;
                op.value = OptionsService.get(key, op.def);
                return op;
            });

            $scope.update = function (opId) {
                var op = _.findWhere($scope.ops, {
                    id: opId
                });

                OptionsService.set(opId, parseValue(
                            op.value,
                            op.type));
            }; 

            console.log("Options", $scope.ops);

        }
    ];

});

模板

<ion-content>

    <p>{{ops}}</p>

    <div class="list">

        <label
         ng-repat="op in ops"
         class="item item-input item-floating-label">
            {{op}}
        </label>

    </div>

console.log显示$ scope.ops是正确的:

Options [Object, Object]

<p>{{ops}}</p>显示json正确。

但是角度编译的结果是:

<div class="scroll">

    <p class="ng-binding">[{"desc":"Max notifications","type":"number","def":10,"min":0,"max":null,"id":"notifications.max-notifications","value":10},{"desc":"Auto sync","def":false,"type":"checkbox","id":"remote.auto-sync","value":false}]</p>

    <div class="list">

        <label ng-repat="op in ops" class="item item-input item-floating-label ng-binding">

        </label>

    </div>

</div>

我缺少什么?

1 个答案:

答案 0 :(得分:1)

这可能有效:

<label ng-repeat="op in ops" 
       class="item item-input item-floating-label ng-binding"
       ng-bind="op">
</label>

另外,如果标签不适用于任何内容,请考虑将标签更改为span。