无法获取要在组件 Angular 6 中使用的 [(ngModel)] 值
cannot get [(ngModel)] value to use in component Angular 6
在 html 文件中,使用 ngModel,我想获取它的值以在我的组件中使用
编辑-customer.component.html
<input id="userInfoEmail" type="text" class="form-control" value="{{userInfo.email}}" [(ngModel)]="userInfo.email" disabled>
由于它的双向绑定,我在我的组件中使用它如下,
编辑-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 的值,先删除它,
<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>
在 html 文件中,使用 ngModel,我想获取它的值以在我的组件中使用
编辑-customer.component.html
<input id="userInfoEmail" type="text" class="form-control" value="{{userInfo.email}}" [(ngModel)]="userInfo.email" disabled>
由于它的双向绑定,我在我的组件中使用它如下,
编辑-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 的值,先删除它,
<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>