0

我想编写一个过滤器,将字符串数组转换为对象数组(这是我最初问题的简化)。所以,['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? . 为什么会出现这个错误?

4

0 回答 0