无法从angularjs中的数组加载数据
问题描述:
我从ng-repeats dupes错误中获取静态。我试过'追踪$索引'无济于事。无法从angularjs中的数组加载数据
以下是我的JSON:
{
"expression": "mithrandir",
"meaning": "language of the Elves",
"example": "",
"pronunciation": "",
"notes": "",
"meta": {
"book": "There and back again",
"author": "Frodo Baggins",
"tags": ["middle earth", "elves"]
}
}
我想填充唯一的代码在下拉。模板如下:
<md-input-container>
<label>Tags</label>
<md-select ng-model="tag">
<md-option ng-repeat="tag in tags" value="{{ tag }}">
{{ tag }}
</md-option>
</md-select>
</md-input-container>
以下是在控制器代码:
classifiedsFactory.getClassifieds().then(function(classifieds) {
$scope.classifieds = classifieds.data;
$scope.tags = getTags($scope.classifieds); // call the getTags method below
// console.log($scope.tags)
});
function getTags(classifieds) {
var tags = [];
angular.forEach(classifieds, function(item) {
angular.forEach(item.meta.tags, function(tag) {
tags.push(tag);
});
});
// console.log(_.uniq(tags))
return _.uniq(tags);
}
我得到以下控制台错误:
这里是github链接: https://github.com/sfumatostar/ngclassifieds/blob/End_of_Section_7/components/classifieds/classifieds.ctr.js
答
的第一个错误可能意味着你的
angular.forEach(classifieds, function(item) {
angular.forEach(item.meta.tags, function(tag) {
tags.push(tag);
});
});
犯规的至少一个项目有一个元场。 因此item.meta是未定义的,因此item.meta.tags会导致“无法读取未定义的属性标记”
第二个和第三个错误实际上确切地说出了问题所在,以及如何处理它。 只需使用
<md-option ng-repeat="tag in tags track by $index" value="{{ tag }}">
{{ tag }}
</md-option>
代替
我怀疑的问题是,你的“公告”项目中的至少一个缺少'meta'场(因此“无法读取未定义的属性”的错误),所以ng-repeat有一些未定义的行(它们被视为愚蠢)。 –