我有一个控制器,其中包含要使用ng-repeat进行迭代的数据,我将它与this selection model directive一起使用。
使用ng-repeat在DOM中创建多个条目后,对$ scope.selectedItems的绑定将停止工作。
我知道ng-repeat为每次迭代创建一个新的子范围,但是不应该通过绑定MyController中的值来获取对selectedItems属性的更新吗?
控制器:
(function() {
angular.module('myModule', ['product.module'])
.controller('MyController' ['$scope', 'SomeService' function($scope, ProductService) {
$scope.selectedItems = [];
$scope.productService = new ProductService() //constructor pattern object
$scope.products = $scope.productService.items;
$scope.productService.fetchData(); //grabs data from server and sets the productService.items to an array of objects
}]);
})();
HTML
<div ng-controller="MyController">
<ul>
<li ng-repeat="product in products"
selection-model-type="checkbox"
selection-model-mode="multiple"
selection-model-selected-items="selectedItems">
{{product.name}}
</li>
</ul>
<div ng-controller="MyController"> <!-- this binding doesn't work after the ng-repeat takes place -->
There are {{selectedItems.length}} products
</div>
答案 0 :(得分:0)
productService.items
可能始终引用同一个实例,但selectedItems
肯定不会。仅使用一个控制器或将selectedItems
放入服务中。
<div ng-controller="MyController">
<ul>
<li ng-repeat="product in products"
selection-model-type="checkbox"
selection-model-mode="multiple"
selection-model-selected-items="selectedItems">
{{product.name}}
</li>
</ul>
There are {{selectedItems.length}} products
</div>