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

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

问题描述

我的 html 标签是:

My html tag is:

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

推荐答案

在 angular 中如果你使用 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文件应该是

then html file should be

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

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

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