我想编写一个过滤器,将字符串数组转换为对象数组(这是我原来问题的简化)。因此,['a', 'b']
应该变为[{id: 'a', label: 'A'}, {id: 'b', label: 'B'}]
。
但是,我的解决方案会导致无限的摘要周期错误。
angular.module('MyTestApp', []).filter('propertiesWithLabels', function() {
return function(properties) {
var propertiesWithLabels = [];
for (var i = 0; i < properties.length; i++) {
propertiesWithLabels.push({
id: properties[i],
label: properties[i].toUpperCase()
});
}
return propertiesWithLabels;
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyTestApp">
<div ng-init="myObject = {properties: ['a','b','c']}">
<ul>
<li ng-repeat="property in myObject.properties | propertiesWithLabels">
{{ property.id }}: {{ property.name }}
</li>
</ul>
</div>
</div>
我不会像在这个问题$rootScope:infdig error caused by filter?中那样修改模型。为什么会出现这种错误?