我有一个数组,它是用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™",
"snippet": "The Next, 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
编辑:
Here is more complicated sample: -> Now的手机集合是空的,当你点击Add按钮时,新的项目被添加并被设置为可编辑的(你可以改变文本字段中的值),当ngRender被执行时,SetPreview函数返回可编辑的对象(它类似于预览)。现在再次尝试单击Add按钮,正如您所看到的,第一项的可编辑值仍然显示给用户,但我想刷新整个ng-repeater。
https://stackoverflow.com/questions/15355122
复制相似问题