Samantha J T Star
Samantha J T Star

Reputation: 32838

How can I set the value of a variable inside a ng-repeat?

I have this ng-repeat:

<div ng-repeat="row in phs.phrasesView = (phs.phrases | orderBy:phs.phrasesOrderBy[phs.phrasesOrderById].key:phs.phrasesSortDirectionId == 1)"

    ***
    *** I want something here to set the value of pos ***
    *** based on array1.findIndex(v => v.phraseId == 'def') + 1; ***
    ***                        
 >
    <div>{{ phs.phrases[pos].phraseId }} </div>
    <div>{{ phs.phrases[pos].keyword }} </div>

I have this data:

var phs.phrases = [{
  "phraseId": "abc",
  "keyword": "bb",
  "posId": 1
}, {
  "phraseId": "def",
  "keyword": "bb",
  "posId": 1
}, ];

What I would like to do is to find the position of the row. I can't use $index as I want to find the position and have it displayed even after sorting.

I know I can use this code to find the pos, but how can I set it up so this code works for each row of the ng-repeat?

var pos = array1.findIndex(v => v.phraseId == 'def') + 1;

Upvotes: 0

Views: 129

Answers (1)

JB Nizet
JB Nizet

Reputation: 692191

Store the position of each object inside the object itself:

phs.phrases.forEach(function(phrase, index) {
    phrase.position = index + 1;
}); 

Or (but his is much less efficient), call a function finding the position inside the ng-repeat:

{{ getPositionOf(row) }}

Upvotes: 1

Related Questions