2016-09-16 71 views
0

我有像這樣的對象數組...角2個ngModel其中id是什麼

this.survey = [ 

     {id: 1, answer: ""}, 
     {id: 2, answer: ""}, 
     {id: 3, answer: ""}, 
     {id: 4, answer: ""}, 
     {id: 5, answer: ""}, 
     {id: 6, answer: ""}, 
     {id: 7, answer: ""}, 
     {id: 8, answer: ""}, 
     {id: 9, answer: ""}, 
     {id: 10, answer: ""}, 
     {id: 11, answer: ""}, 
     {id: 12, answer: ""}, 
     {id: 13, answer: ""}, 
     {id: 14, answer: ""}, 
     {id: 15, answer: ""}, 
     {id: 16, answer: ""}, 
     {id: 17, answer: ""}, 
     {id: 18, answer: ""}, 
     {id: 19, answer: ""}, 
     {id: 100, answer: ""}, 
     {id: 101, answer: ""} 

    ]; 

我怎樣才能綁定到該對象其中id = 101以下文本區域的價值?

<textarea name="comments" class="form-control" (change)="updateSurvey($event, 101)" 
[(ngModel)]="survey.?????"></textarea> 

我知道,通常它可以通過了解索引位置easlily做,但我不能總是依賴於這樣我怎麼能綁定到其中id = 101在這種情況下?相對於[(ngModel)] = 「調查[21]」

由於

回答

0

我假定它不是可能的,而不使用索引。

可能的路要走:

<textarea name="comments" class="form-control" (change)="updateSurvey($event, 101)" 
[value]="getSurveyAnswer(101)"></textarea> 

在組件:

getSurveyAnswer(id: number): string { 
    return this.survey.find(s => s.id == id).answer; 
} 
+0

感謝爲解決! :)我的速度很快.. – mxii