[英]cannot get [(ngModel)] value to use in component Angular 6
在html文件中,使用ngModel,我想获取其值以在组件中使用
edit-customer.component.html
<input id="userInfoEmail" type="text" class="form-control" value="{{userInfo.email}}" [(ngModel)]="userInfo.email" disabled>
由于它是双向绑定的,因此我在组件中按如下方式使用它,
edit-customer.component.ts
checkUserEmail(): void {
this.userInfo.email = this.userEmail;
this.customerService.checkEmail(this.userEmail).subscribe((res) => {
if (res.twoFactorEnabled === true) {
this.isButtonDisabled = false;
}
else {
this.isButtonDisabled = true;
}
})
}
我也声明了this.userEmail:string;
,但不幸的是我的控制台出现错误“ undefined”,我读到我需要初始化该对象,但无法弄清楚,
不要在ngModel中使用value,请先将其删除,
<input id="userInfoEmail" type="text" class="form-control" [(ngModel)]="userInfo.email" disabled>
现在您应该能够访问控制器中的值,例如
console.log(this.userInfo.email);
而userInfo应该在最上方被捍卫,
userInfo: any = {};
如果您有类型更改,请更改任何类型
你也可以那样做
ts文件中的功能
get_coin_current_market_value(symbol){
console.log('symbol is ',symbol);
}
和HTML
<select class="form-control" name="coin" [(ngModel)]="symbol"
(ngModelChange)="get_coin_current_market_value(symbol)">
<option *ngFor="let coin of allCoinsArray">
{{ coin.symbol }}
</option>
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.