Angular 2 ngModel,其中Id是某种东西

时间:2016-09-16 20:49:15

标签: angular typescript

我有一个像这样的对象数组......

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>

我知道通常可以通过轻松了解索引位置来完成,但我不能总是依赖它,那么在这种情况下如何绑定到id = 101的位置?而不是[(ngModel)] =“survey [21]”

由于

1 个答案:

答案 0 :(得分: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;
}