简体   繁体   English

无法获取[(ngModel)]值以在组件Angular 6中使用

[英]cannot get [(ngModel)] value to use in component Angular 6

in html file, using ngModel which i want to get its value to use in my component 在html文件中,使用ngModel,我想获取其值以在组件中使用

edit-customer.component.html edit-customer.component.html

  <input id="userInfoEmail" type="text" class="form-control" value="{{userInfo.email}}" [(ngModel)]="userInfo.email" disabled>

since its two way binding, i had use it im my component as follows, 由于它是双向绑定的,因此我在组件中按如下方式使用它,

edit-customer.component.ts 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;
        }
    })
}

also I had declared this.userEmail:string; 我也声明了this.userEmail:string; , but unfortunately got error 'undefined' on my console, I read that i need to initialize the object but cant figure it out, ,但不幸的是我的控制台出现错误“ undefined”,我读到我需要初始化该对象,但无法弄清楚,

Do not use value with ngModel, remove it first, 不要在ngModel中使用value,请先将其删除,

  <input id="userInfoEmail" type="text" class="form-control" [(ngModel)]="userInfo.email" disabled>

now you should be able to access the value in controller like, 现在您应该能够访问控制器中的值,例如

console.log(this.userInfo.email);

whereas userInfo should be defiend at the top as, 而userInfo应该在最上方被捍卫,

userInfo: any = {}; if you have a type change any with type 如果您有类型更改,请更改任何类型

you can also did that 你也可以那样做

in ts file make a function ts文件中的功能

  get_coin_current_market_value(symbol){
console.log('symbol is ',symbol);

} }

and in html 和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