2016-08-02 33 views
0

我有这个NG-重复:如何在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> 

我有这样的数据:

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

我想这样做的是找到行的位置。我不能使用$索引,因为我想查找位置并在排序后显示它。

我知道我可以使用这段代码找到pos,但是我怎样才能设置它,所以这段代码适用于ng-repeat的每一行?

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

如果您不是指'$ index',那么您不清楚“行的位置”是什么意思。对数组进行排序可更改项目的位置;如果你以某种方式在排序之前存储了索引,那么在排序之后显示该值,这将不是一个有用的值,因为该项目不再需要在该阵列位置。如果由于某种原因需要每个对象具有固定值,那么该值将需要是每个对象的属性。 – Claies

+1

将每个对象的位置存储在对象内部:'phs.phrases.forEach(function(phrase,index){phrase.position = index + 1;});'或者(效率低得多),调用函数在ng-repeat中的位置:'{{getPositionOf(row)}}' –

+0

@JBNizet - 您能否提出您的解决方案作为答案。谢谢 –

回答

1

存储对象本身内部的每个对象的位置:

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

或(不过他是效率要低得多),调用一个函数找到NG重复内的位置:

{{ getPositionOf(row) }} 
+0

谢谢。这工作得很好 –