AngularJS:ng之后,重复$scope值未按预期更新
AngularJS : After ng-repeat $scope values not updated as expected
我有一个控制器,它包含要用ng repeat迭代的数据,我将它与这个选择模型指令一起使用。
在使用ng repeat在DOM中创建多个条目后,到$scope.selectedItems的绑定将停止工作。
我知道ng repeat为每个迭代创建一个新的子作用域,但对selectedItems属性的更新不应该仍然通过绑定到MyController中的值来获取吗?
控制器:
(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>
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>
相关文章:
- ng在更新$scope后重复不更新信息
- 获取Div Width,然后更新Scope Variable
- 在从websocket服务器推送消息后更新$scope变量
- JavaScript未更新$scope变量的一部分
- 如何在Angular中传递此回调并更新$scope
- 离子角 单击按钮后更新$scope变量
- AngularJS:如何在承诺响应后更新$scope
- AngularJS更新$scope变量并输出到pre
- 从 AngularJS 中的服务属性更新$scope
- 更新$scope-dons'不要影响视图
- AngularJS控制器在后端数据更改时自动更新$scope
- 从AngularJS中的服务获取数据后,如何更新$scope是最好的方法
- Angular js 在
标签上的变量更新$scope动画
- 为什么在这种特殊情况下视图没有更新?($scope.$apply)
- 通过更新$scope模型更正条目后,清除角度形式验证
- 当contenteditable Elements使用Angular.js更改时,更新$scope
- Angularjs-在$resource-delete方法的回调中更新$scope的问题(thinkster.io第三章
- Angular JS,在指令中从控制器中更新$scope
- 关闭模式时更新$scope集合
- Ionic:更新$scope变量后重新加载视图