使用ngInit – ngRepeat的angularjs – ngRepeat不会刷新渲染值
发布时间:2020-12-17 08:06:16 所属栏目:安全 来源:网络整理
导读:我有使用ngRepeater显示的数组,但是使用这个指令,我使用ngInit指令执行函数,该函数应该返回要显示的对象。一切正常工作,但是当我添加“新”按钮时,我将新值添加到数组,然后功能“SetPreview”只有在我认为应该根据数组值的数量执行功能时才执行。我怎
我有使用ngRepeater显示的数组,但是使用这个指令,我使用ngInit指令执行函数,该函数应该返回要显示的对象。一切正常工作,但是当我添加“新”按钮时,我将新值添加到数组,然后功能“SetPreview”只有在我认为应该根据数组值的数量执行功能时才执行。我怎样才能做到这一点?
用户界面: <body ng-app> <ul ng-controller="PhoneListCtrl"> <button ng-click="add()">Add</button> <li ng-repeat="phone in phones" ng-init="displayedQuestion=SetPreview(phone);"> {{displayedQuestion.name}} <p>{{displayedQuestion.snippet}}</p> </li> </ul> </body> 控制器: function PhoneListCtrl($scope) { $scope.phones = [ {"name": "Nexus S","snippet": "Fast just got faster with Nexus S."},{"name": "Motorola XOOM? with Wi-Fi","snippet": "The Next,Next Generation tablet."},{"name": "MOTOROLA XOOM?",Next Generation tablet."} ]; $scope.add = function(){ this.phones.push({name:'1',snippet:'n'}); }; $scope.SetPreview = function(phone) { //here logic which can take object from diffrent location console.log(phone); return phone; }; } Sample is here – jsfiddle 编辑:
你正在进行一个角色表演功能。
基本角度可以看到数组中的元素(例如’A’)是相同的对象引用,所以它不会再次调用ng-init。这是有效的。即使您将一个旧列表连接到一个新的列表中,Angular也会看到它是相同的引用。 如果相反,您创建一个与旧对象具有相同值的新对象,则它具有不同的引用,并且Angular重新引用它: $scope.add = function(item) { var newItems = []; angular.forEach($scope.items,function(obj){ this.push({val:obj.val}); },newItems) newItems.push({val:item}) $scope.items = newItems; }; 我不推荐在小提琴中采用这种方法,而是应该找到一种不同的方法,ng-init来触发你的代码。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |