绑定元素中的值

时间:2018-11-09 14:14:57

标签: angular two-way-binding

我正在尝试从“ a”元素绑定值:

-template.component.html:

  <form>
    <ul *ngFor="let city of cities">
      <a (click)="directToViewByCity()" [(ngModel)]="city.city" name="name">{{city.city}}  </a>
    </ul>
</form>

- template.component.ts:

directToViewByCity(selectedCity){
    this._router.navigate(['viewByCity'])

    var selectedCity=selectedCity;
    console.log(selectedCity)
  }

要分配给component.ts中的变量selectedCity,但是我遇到此错误:

core.js:1673 ERROR Error: Uncaught (in promise): Error: No value accessor for form control with name: 'name' Error: No value accessor for form control with name: 'name'

我尝试了几种解决方案,并且在互联网上进行了研究,但是我还没有找到合适的解决方案。

1 个答案:

答案 0 :(得分:1)

您不需要form和ngModel,只需执行以下操作即可:

<ul *ngFor="let city of cities">
    <a (click)="directToViewByCity(city)">{{city.city}}</a>
</ul>