簡體   English   中英

無法獲取[(ngModel)]值以在組件Angular 6中使用

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM