在json对象上使用ng-repeat

时间:2018-05-23 15:36:39

标签: angularjs

我刚开始使用angularJS而且我的ng-repeat有问题。因此,在我的component.js文件中,我将$scope声明为vm并声明了一个对象数组:

vm.locOptionFields =
        {
            "locOptionFieldKey": 1,
            "fieldID": "Allow additional material to be added to Orders that have been processed but not shipped",
            "stringNum": 11,
            "taskKey": 12,
            "type": "checkbox",
            "seqNum": 13,
            "comboOptionRefKey": 1
        },
        {
            "locOptionFieldKey": 2,
            "fieldID": "Require requested delivery date",
            "stringNum": 12,
            "taskKey": 13,
            "type": "number",
            "seqNum": 14,
            "comboOptionRefKey": 1
        },
        {
            "locOptionFieldKey": 3,
            "fieldID": "Default Requested Delivery Date into Promise Date",
            "stringNum": 13,
            "taskKey": 14,
            "type": "combobox",
            "seqNum": 15,
            "comboOptionRefKey": 2
        };

我想在我的.html文件中引用该信息,并使用ng-repeat打印出每个对象的taskKey。我试过这样做:

<li ng-repeat="loc in vm.locOptionFields">
    {{loc.type}}
</li>

但所有正在做的就是给我空白列表项,每个元素对应一个元素。但是,当我打印出{{loc}}时,它会打印出每个项目,仅针对第一个对象。它只是出于某种原因才识别第一个对象吗?

1 个答案:

答案 0 :(得分:0)

您没有使用数组。你遗漏了对象声明周围的[]

添加它们并按预期工作:http://jsfiddle.net/g6dn0j7o/