ng-repeat: access key and value for each object in array of objects

前端 未结 6 681
清歌不尽
清歌不尽 2020-12-04 15:06

I have an array of objects and I am using an ng-repeat to iterate over them, which is easy. The array looks something like this:

$scope.steps =         


        
相关标签:
6条回答
  • 2020-12-04 15:44

    Here is another way, without the need for nesting the repeaters.

    From the Angularjs docs:

    It is possible to get ngRepeat to iterate over the properties of an object using the following syntax:

    <div ng-repeat="(key, value) in steps"> {{key}} : {{value}} </div>
    
    0 讨论(0)
  • 2020-12-04 15:54

    seems like in Angular 1.3.12 you do not need the inner ng-repeat anymore, the outer loop returns the values of the collection is a single map entry

    0 讨论(0)
  • 2020-12-04 15:56

    In case this is an option for you, if you put your data into object form it works the way I think you're hoping for:

    $scope.steps = {
     companyName: true,
     businessType: true,
     physicalAddress: true
    };
    

    Here's a fiddle of this: http://jsfiddle.net/zMjVp/

    0 讨论(0)
  • 2020-12-04 15:59

    I think the problem is with the way you designed your data. To me in terms of semantics, it just doesn't make sense. What exactly is steps for?

    Does it store the information of one company?

    If that's the case steps should be an object (see KayakDave's answer) and each "step" should be an object property.

    Does it store the information of multiple companies?

    If that's the case, steps should be an array of objects.

    $scope.steps=[{companyName: true, businessType: true},{companyName: false}]
    

    In either case you can easily iterate through the data with one (two for 2nd case) ng-repeats.

    0 讨论(0)
  • 2020-12-04 16:04

    A repeater inside a repeater

    <div ng-repeat="step in steps">
        <div ng-repeat="(key, value) in step">
            {{key}} : {{value}}
        </div>
    </div>
    
    0 讨论(0)
  • 2020-12-04 16:04

    In fact, your data is not well design. You'd better use something like :

    $scope.steps = [
        {stepName: "companyName", isComplete: true},
        {stepName: "businessType", isComplete: true},
        {stepName: "physicalAddress", isComplete: true}
    ];
    

    Then it is easy to do what you want :

    <div ng-repeat="step in steps">
     Step {{step.stepName}} status : {{step.isComplet}}
    </div>
    

    Example: http://jsfiddle.net/rX7ba/

    0 讨论(0)
提交回复
热议问题