【问题标题】:cannot get [(ngModel)] value to use in component Angular 6无法获得 [(ngModel)] 值以在组件 Angular 6 中使用
【发布时间】:2019-01-16 17:00:58
【问题描述】:

在 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;,但不幸的是在我的控制台上出现错误“未定义”,我读到我需要初始化对象但无法弄清楚,

【问题讨论】:

  • 如果你有[(ngModel)],你就不需要value="{{userInfo.email}}",另外,你在组件中声明了userInfo:string = "";吗?

标签: angular typescript angular6 primeng angular-ngmodel


【解决方案1】:

不要对ngModel使用value,先去掉,

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

现在您应该能够访问控制器中的值,例如,

console.log(this.userInfo.email);

而 userInfo 应该在顶部被定义为,

userInfo: any = {};如果您有类型,请更改任何类型

【讨论】:

    【解决方案2】:

    你也可以这样做

    在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>
    

    【讨论】:

      猜你喜欢
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      • 2019-07-22
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      相关资源
      最近更新 更多