angularjs – 如何$观看由ng-repeat创建的模型的更改?
发布时间:2020-12-17 08:41:49 所属栏目:安全 来源:网络整理
导读:例如考虑 this Plnkr。我不知道将会创建多少fooCollection的成员。所以我不知道有多少条模型会存在。 但我知道他们将是有角度的模型,我知道他们会在哪里。 我如何做一个$手表这些? 我需要这样做,因为我需要在改变条形模型时触发行为。观察fooCollection本
例如考虑
this Plnkr。我不知道将会创建多少fooCollection的成员。所以我不知道有多少条模型会存在。
但我知道他们将是有角度的模型,我知道他们会在哪里。 我如何做一个$手表这些? 我需要这样做,因为我需要在改变条形模型时触发行为。观察fooCollection本身是不够的,当改变条形时,$ watch监听器不会触发。 相关html: <body ng-controller="testCtrl"> <div ng-repeat="(fooKey,foo) in fooCollection"> Tell me your name: <input ng-model="foo.bar"> <br /> Hello,my name is {{ foo.bar }} </div> <button ng-click="fooCollection.push([])">Add a Namer</button> </body> 相关JS: angular .module('testApp',[]) .controller('testCtrl',function ($scope) { $scope.fooCollection = []; $scope.$watch('fooCollection',function (oldValue,newValue) { if (newValue != oldValue) console.log(oldValue,newValue); }); });
创建单个列表项目控制器:
demo on Plnkr
js angular .module('testApp',[]) .controller('testCtrl',function ($scope) { $scope.fooCollection = []; }) .controller('fooCtrl',function ($scope) { $scope.$watch('foo.bar',function (newValue,oldValue) { console.log('watch fired,new value: ' + newValue); }); }); HTML <html ng-app="testApp"> <body ng-controller="testCtrl"> <div ng-repeat="(fooKey,foo) in fooCollection" ng-controller="fooCtrl"> Tell me your name: <input ng-model="foo.bar" ng-change="doSomething()"> <br /> Hello,my name is {{ foo.bar }} </div> <button ng-click="fooCollection.push([])">Add a Namer</button> </body> </html> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |