angular - 角度 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 -->
解决方案
首先,你不能用 html 中的 this.x 访问。
<li class="dropdownfilter" *ngIf="arr.inclues('Male')" (click)="getValueGender('Male',1)"><a>Male</a></li>
其次,li 标签没有 name 属性也不支持 [(ngModel)] 因为它需要 value 属性。ngModel 是一个有角度的内置指令。双向绑定使用 [()] 语法,属性绑定使用 [] 语法,事件绑定使用括号 () 语法。
推荐阅读
- r - R Simmer - 应用于使用循环构建的模拟环境,从而产生相同的模拟
- c - 编译c文件后得到什么?
- python - 如何创建一个具有不能被其子类继承的公共方法的类?
- jenkins - 如何在 Jenkins 管道中编写“Git LFS pull after checkout”设置
- cassandra - Cassandra 如何从 SStable 中检索数据并将其合并到 memetable 中?这些数据会再次刷新吗?
- c++ - 为犰狳矩阵赋值
- python - 如何使用 1d 数组搜索 2d 数组以返回 2d 数组的索引 1
- c# - 有没有办法从 Bot Framework 机器人发送 GroupMe 的图像或位置附件?
- javascript - 错误的链接,错误的地方
- angularjs - 如何在 Jasmine 中对 $uibModal 进行单元测试?(单元测试注入库)