1

I have an array of objects like following:

subgoals: any= [
    {'index':'0', 'title': 'Subgoal 1', 'priority': 'Max'},
    {'index':'1', 'title': 'Subgoal 2', 'priority': 'Low'},
    {'index':'2', 'title': 'Subgoal 3', 'priority': 'Avg'},
    {'index':'3', 'title': 'Subgoal 4', 'priority': 'Max'},
]

and two properties:

title:string='';
priority:string='';

I iterate over this array of objects on my template:

<div *ngFor=" let chunk of subgoals | chunks: 1; let j = index; " class="mdl-cell mdl-cell--4-col subgoal-card mdl-card">
    <div *ngFor=" let subgoal of chunk | values" >
        <div class="mdl-card__title">
            <h6>{{subgoal.title}}</h6>
        </div>

        <div class="mdl-card__actions">
            <button class="mdl-button" (click)="openSubgoalForEditing(j)">
                View
            </button>
        </div>
    </div>
</div>

<div>{{title}}</div>
<div>{{priority}}</div>

Now depending on the index received in my openSubgoalForEditing(j) I would like iterate over my subgoals[] array to get the corresponding object and then set title and priority which I can show in my template back.

How can I do this?

Thinker
  • 5,326
  • 13
  • 61
  • 137
  • Iterating through and object in the template: http://stackoverflow.com/questions/37431578/iteration-a-json-object-on-ngfor-in-angular-2 . I used this, worked like a charm! ;) – SrAxi Apr 04 '17 at 14:03

1 Answers1

0

I applied a simple for loop:

for(let item of this.subgoals){
    if(item.index==index){

        this.title=item.title;
        this.priority=item.priority
    }
}   
Thinker
  • 5,326
  • 13
  • 61
  • 137