0

Updated

My question is how to loop the particular array by providing array index data-ng-repeat="scenarios in input.model.Scenarios[1]" instead of looping all arrays data-ng-repeat="scenarios in input.model.Scenarios" ?

For example :

Let's say i have array like this

Scenario[0]
    SceId
    SceName
    Sections[0]
        LicencePlates[0]
            LpId
            LpName
        LicencePlates[0]
            LpId
            LpName
Scenario[1]
    SceId
    SceName
    Sections[0]
        LicencePlates[0]
            LpId
            LpName
        LicencePlates[0]
            LpId
            LpName
Scenario[2]
    SceId
    SceName
    Sections[0]
        LicencePlates[0]
            LpId
            LpName
        LicencePlates[0]
            LpId
            LpName

Angular Code

<div data-ng-app="cpa" data-ng-controller="InputController as input">
    <div data-ng-repeat="scenarios in input.model.Scenarios">
        <div data-ng-repeat="sections in scenarios.Sections">
            <div data-ng-repeat="licensePlates in sections.LicensePlates">
            </div>
        </div>
    </div>
</div>

For above, i'm looping through all array and getting result.

<div data-ng-app="cpa" data-ng-controller="InputController as input">
    <div data-ng-repeat="scenarios in input.model.Scenarios[1]">
        <div data-ng-repeat="sections in scenarios.Sections">
            <div data-ng-repeat="licensePlates in sections.LicensePlates">
            </div>
        </div>
    </div>
</div>

But for above code when i use Scenarios[1] it's loops Scenarios[1] not getting inside Sections[0] and LicencePlates[0].

Output:

SceId : 2
SceName : Scenario2

In real time i'm passing parent $index as array index

<div data-ng-repeat="scenarios in input.model.ParentScenarios" ng-init="ParentScenarioIndex = $index">

<div data-ng-repeat="scenarios in input.model.Scenarios[ParentScenarioIndex]">

6
  • just sectionIndex without {{ .. }}? Commented Sep 12, 2015 at 4:46
  • I had update the code and output. I'm not getting proper output. Please help out. Commented Sep 12, 2015 at 6:18
  • I can't figure your 6 levels depth ng-repeat, pls post some virtual data, so someone can figure it. Commented Sep 12, 2015 at 6:23
  • This doesn't really make any sense as it is currently presented. It's not obvious what these numbers are supposed to represent, what the data you are iterating through is structured like, or what the relationship between each level here is supposed to be. Also, if grouping and ordering are important to you, you should have an index property on your object, rather than trying to rely upon the view $index angular assigns. In general, don't try to do calculations in the view. Commented Sep 12, 2015 at 6:27
  • Seems like you've over complicated your logic, this is really difficult to work with, scale and maintain in the future. You should consider changing it to be easier to handle. Commented Sep 12, 2015 at 7:46

1 Answer 1

1

The problem in the second case is that you want to loop an object with ng-repeat.

data-ng-repeat="scenarios in input.model.Scenarios[1]"

In this case input.model.Scenarios[1] is an object and ng-repeat will loop through it's properties values. Example:

// for 
var object = { a: 'aa', foo: 'bar' };
// and
data-ng-repeat="value in object"
// value will receive 'aa' and 'bar'

If you want to show the data only for the first object you can do it like this:

<div data-ng-app="cpa" data-ng-controller="InputController as input">
    <div data-ng-init="scenario = input.model.Scenarios[1]">
        <div data-ng-repeat="sections in scenario.Sections">
            <div data-ng-repeat="licensePlates in sections.LicensePlates">
            </div>
        </div>
    </div>
</div>

I hope that this answer will be helpful for you. For more info you can check angular documentation for ng-repeat or write me a comment.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. I will check it and revert it back

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.