AngularJS 个重复范围
AngularJS ng-repeat scope
正在尝试将 ng-repeat
用于字符串数组。我有 2 种情况,1 种有效,另一种无效。我的问题是为什么它不起作用?
在这种情况下相应地显示了家庭类型:
工作
<div ng-controller="productsController as product">
<ul>
<li ng-repeat="family in product.families | orderBy: 'category'">
<ul>
<li ng-repeat="type in family.types">{{type}}</li>
</ul>
</li>
</ul>
</div>
但是,在这种情况下,它们不会显示:
不工作 为什么?
<div ng-controller="productsController as product">
<ul>
<li ng-repeat="family in product.families | orderBy: 'category'">
</li>
</ul>
<ul>
<li ng-repeat="type in product.families.types">{{type}}</li>
</ul>
</div>
在这种情况下,重复类型的正确方法是什么?
JSON
"families": [
{
"category": "Tablets",
"types": ["type1", "type2", "type3", ...]
}
]
product.families
是一个数组。数组没有任何名为 types
.
的 属性
适当的方法是在控制器中提取一个数组,该数组将包含所有系列的所有类型作为单个数组,并迭代该单个数组中包含的类型:
var typeArrays = $scope.families.map(function(family) {
return family.types;
});
$scope.allTypes = [].concat.apply([], typeArrays);
正在尝试将 ng-repeat
用于字符串数组。我有 2 种情况,1 种有效,另一种无效。我的问题是为什么它不起作用?
在这种情况下相应地显示了家庭类型: 工作
<div ng-controller="productsController as product">
<ul>
<li ng-repeat="family in product.families | orderBy: 'category'">
<ul>
<li ng-repeat="type in family.types">{{type}}</li>
</ul>
</li>
</ul>
</div>
但是,在这种情况下,它们不会显示: 不工作 为什么?
<div ng-controller="productsController as product">
<ul>
<li ng-repeat="family in product.families | orderBy: 'category'">
</li>
</ul>
<ul>
<li ng-repeat="type in product.families.types">{{type}}</li>
</ul>
</div>
在这种情况下,重复类型的正确方法是什么?
JSON
"families": [
{
"category": "Tablets",
"types": ["type1", "type2", "type3", ...]
}
]
product.families
是一个数组。数组没有任何名为 types
.
适当的方法是在控制器中提取一个数组,该数组将包含所有系列的所有类型作为单个数组,并迭代该单个数组中包含的类型:
var typeArrays = $scope.families.map(function(family) {
return family.types;
});
$scope.allTypes = [].concat.apply([], typeArrays);