Ngrepeat does not iterate over $ attribute

I tried filtering with an object and printing the filters through ng-repeat = (key, value)

in the object.

As I tried various filters , I saw that ng-repeat

it doesn't seem to work with an $

object attribute , which is pretty useful if you're filtering.

Is it possible to automatically display all the attributes of filter objects even if you use $

This link shows that it doesn't work with objects starting with $

  $scope.testObj = {};

  $scope.testObj.test = 'test';
  $scope.testObj.$ = '$';
  $scope.testObj.$test = '$test';

  <div ng-repeat = "(key, value) in testObj">
    <p>{{key}}: {{value}}</p>
  </div>

      

+3


source to share


3 answers


AngularJS doesn't support it yet. There is an open issue on Github .

However, you can make it work with a little code:

app.controller('MainCtrl', function($scope) {

  var getProperties = function(input){
     var result = [];

     for(var propertyName in input) {
       result.push({key : propertyName, value : input[propertyName]});
     }

    return result;
  };


  $scope.testObj = {};
  $scope.testObj.test = 'test';
  $scope.testObj.$ = '$';
  $scope.testObj.$whatever = '$whatever';

  $scope.testObjProperties = getProperties($scope.testObj);

});

      



Then display it in your view:

<div ng-repeat="property in testObjProperties">
    <p>{{property.key}} : {{property.value}}</p>
</div>

      

Here's the working panel: http://plnkr.co/edit/LFrfLcpoOg0ScEY89p25?p=preview

+2


source


It looks like it ng-repeat

filters the properties of the object starting with $

.

This is from the source :

    for (var itemKey in collection) {
      if (collection.hasOwnProperty(itemKey) && itemKey.charAt(0) != '$') {
        collectionKeys.push(itemKey);
      }
    }

      



This is most likely due to what Angular uses $

to specify code that is internal to the Angular library.

This seems to only happen if you are using ng-repeat

over an object.

+2


source


I can see that they are explicitly prohibited in the code:

https://github.com/angular/angular.js/blob/master/src/ng/directive/ngRepeat.js#L341

There are no comments, and I agree that this looks more like a bug than a function to me. Doesn't that mean that for properties the markings are not enumerable and Object.keys

for? Browser compatibility hell, may be the reason, as always.

0


source







All Articles