How to iterate over the keys and values with ng-repeat in AngularJS?

后端 未结 9 1660
抹茶落季
抹茶落季 2020-11-22 04:36

In my controller, I have data like: $scope.object = data

Now this data is the dictionary with keys and values from json.

I can acce

9条回答
  •  醉话见心
    2020-11-22 05:06

    A todo list example which loops over object by ng-repeat:

    var app = angular.module('toDolistApp', []);
    app.controller('toDoListCntrl', function() {
      var self = this;
      self.toDoListItems = {};// []; //dont use square brackets if keys are string rather than numbers.
      self.doListCounter = 0;
    
      self.addToDoList = function() {		  		   
        var newToDoItem = {};
        newToDoItem.title     = self.toDoEntry;
        newToDoItem.completed = false;		   
    
        var keyIs = "key_" + self.doListCounter++;  		   
    
        self.toDoListItems[keyIs] = newToDoItem;		   
        self.toDoEntry = ""; //after adding the item make the input box blank.
      };
    });
    
    app.filter('propsCounter', function() {
      return function(input) {
        return Object.keys(input).length;
      }
    });
    
        
      
    Total Items: {{toDoListCntrlAs.toDoListItems | propsCounter}}
    Enter todo Item: {{toDoListCntrlAs.toDoEntry}}
    {{$index+1}} : {{key}} : Title = {{ prop.title}} : Status = {{ prop.completed}}

提交回复
热议问题