首页 > 解决方案 > 角度 5 中未指定名称属性的表单控件没有值访问器

问题描述

HTML 代码

<li class="dropdownfilter" *ngIf="this.arr.inclues('Male')" (click)="getValueGender('Male',1,)" [(ngModel)]="M"><a>Male</a></li>

我面临以下异常

ERROR Error: No value accessor for form control with unspecified name attribute

所以我添加了

<li class="dropdownfilter" name="gendermale" *ngIf="this.arr.inclues('Male')" (click)="getValueGender('Male',1,)" [(ngModel)]="M"><a>Male</a></li>

现在面临以下异常

ERROR Error: No value accessor for form control with name: 'gendermale'

我在代码的许多地方都面临着这个异常。

<div class="col-sm-8 col-xs-8 contact">
    <input  id="passphn{{bus.busServiceId}}" (keyup)="onKeyPress($event)" class="form-control contact-number" type="text" [(ngModel)]="contactNumber" name="contact-number" placeholder="Phone" required pattern="(?<!\d)\d{10}(?!\d)" minlength="10" maxlength="10" />
    <div class="shake-tooltip-web-mobile" *ngIf="webMobileError != ''">{{webMobileError}}</div> <!-- Facing the same exception here -->
</div>

<div class="panel-body" *ngIf="dropingView == 'show'"> <!-- Here too -->

标签: angulartypescriptangular-ngmodel

解决方案


首先,你不能用 html 中的 this.x 访问。

<li class="dropdownfilter" *ngIf="arr.inclues('Male')" (click)="getValueGender('Male',1)"><a>Male</a></li>

其次,li 标签没有 name 属性也不支持 [(ngModel)] 因为它需要 value 属性。ngModel 是一个有角度的内置指令。双向绑定使用 [()] 语法,属性绑定使用 [] 语法,事件绑定使用括号 () 语法。


推荐阅读