为什么我们不能在角度6的同一输入标签中使用[[ngModel)]和值 [英] Why can't we use [(ngModel)] and value in same input tag in angular 6

查看:339
本文介绍了为什么我们不能在角度6的同一输入标签中使用[[ngModel)]和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的html标签是:

<input matInput placeholder="Vehicle Number" name="vehicleNo" [(ngModel)]="vehicleNo" value="vehicle.vehicleNo" >

我需要自动填写输入字段,如果我输入新内容,则需要在component.ts文件中进行访问.

I need to automatically fill the input field and if i enter new thing it need to access in component.ts file.

推荐答案

如果使用ngModel绑定数据,则不需要使用value属性,它将自动绑定数据.

In angular if you are using ngModel to bind data then you don't need to use value attribute, it will automatically bind the data.

//在ts文件中

export className extends OnInit{
    vehicle: any;

    ngOnInit(){

       this.serviceName.functionName().subscribe(
           data=>{
               this.vehicle=data;
           }error=>{
               //whatever logic you want to place
          }
      );
   }
}

//在html文件中

<input matInput placeholder="Vehicle Number" name="vehicleNo" [(ngModel)]="vehicle.vehicleNo">

//如果以这种方式定义它vehicleObj = { vehicleNo:this.vehicleNo }

// if you are defining it this way vehicleObj = { vehicleNo:this.vehicleNo }

然后html文件应为

<input matInput placeholder="Vehicle Number" name="vehicleNo" [(ngModel)]="vehicleObj .vehicleNo">

这篇关于为什么我们不能在角度6的同一输入标签中使用[[ngModel)]和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆