具有FormControl和NgModel问题的Angular mat-form-field

问题描述 投票:1回答:1

我有一个带有mat-form-field输入框的type="number"

我不确定我是否应该使用FormControl或NgModel。

我有一个子组件,recieves是一个Input对象,我应该将输入字段中写入的内容保存到此对象的属性中。

这是我的孩子控制器:

@Input()
  building: Building;  

@ViewChild("numberMatInput", {read: MatInput})
  numberMatInput: MatInput;
  numberInput: FormControl = new FormControl();

ngOnInit() {
    this.numberInput.valueChanges
    .subscribe(s => {
      this.building.radius = s;
    });
}

我的观点是:

<mat-form-field appearance="outline">
     <mat-label>KM</mat-label>
     <input type="number" matInput #numberMatInput [formControl]="numberInput">
</mat-form-field>
<mat-icon matListIcon (click)="numberInput.setValue('')">close</mat-icon>

所以它工作,输入值被保存到对象属性 - > this.building.radius,但什么不起作用,这是一个面板,可以在选择建筑物时打开,如果我关闭面板并再次打开它,输入字段在关闭面板之前,它是空的而不是显示最新值。

我应该使用NgModel吗?所以我可以直接使用NgModel building.radius

对不起,我对Angular很新!

angular angular-ngmodel form-control
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.