简体   繁体   English

如何绑定到Angle 2中的特定数组项?

[英]how can i bind to a specific array item in angular 2?

I want to use data that has been set in an array to bind to specific input boxes. 我想使用在数组中设置的数据绑定到特定的输入框。 with this piece of code, I get an error: 这段代码,我得到一个错误:

Recommendation.question contains an array of questions that has been loaded through a service. Recommendation.question包含通过服务加载的一系列问题。 This works fine, and also if just show the value of question by using the brackets it works. 这可以很好地工作,并且也可以使用方括号显示问题的价值。 Just the value of the array won't bind to the input box. 只是数组的值不会绑定到输入框。

The error I Get: 我得到的错误:

Unhandled Promise rejection: Cannot assign to a reference or variable! ; Zone: <root> ; Task: Promise.then ; Value: ZoneAwareError Error: Cannot assign to a reference or variable!

  <div class="list-group-item"
    *ngFor="let question of Recommendation.question; let i = index;">
    <input class="form-control" name="question" [(ngModel)]="question"/>
    {{question}}
    {{i}}
  </div>

if I comment out the input field, the {{question}} shows the correct value that is at the current index of the array. 如果我注释掉输入字段,则{{question}}显示数组当前索引处的正确值。

The problem should lie with that the [(ngModel)] variable is the same as the reference in the iteration, meaning question . 问题在于[(ngModel)]变量与迭代中的引用相同,即question

Doing this should work: 这样做应该可以:

<div class="list-group-item"
  *ngFor="let question of Recommendation.question; let i = index;">
  <input class="form-control" name="question" [(ngModel)]="Recommendation[i]"/>
  {{question}}
  {{i}}
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM