我正在使用 Angular,我想做的是将过滤后的集合传递给指令。
我的观点是这样的:
<h1>Filtered Collection (no directive)</h1>
<ul>
<li ng-repeat="person in filteredPeople = (people | filter: { isChecked: true })">
{{ person.name }}
</li>
</ul>
我传递的数据只是一个简单的对象数组:
$scope.people = [
{
name: "George",
isChecked: false
},
{
name: "Jane",
isChecked: false
},
{
name: "Peter",
isChecked: true
}
];
到目前为止一切正常。但是当我尝试将上面的 HTML 放在指令中时,应用程序崩溃了。
指示:
myApp.directive('filterPeople', function () {
return {
restrict: 'A',
template: '<h1>Filtered Collection (directive)</h1>' +
'<ul>' +
'<li ng-repeat="item in collection">{{ item.name }}</li>' +
'</ul>',
scope: {
collection: '=collection'
}
}
});
看法:
<div filter-people collection="filteredPeople"></div>
我得到的错误:
Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations
PS 我已经在Plunker上传了我的示例,一切似乎都在那里工作。我检查了我的应用程序中的代码是否相同。那么,为什么会这样呢?我的代码在Plunker上运行,但不在我的真实应用程序中。