2

以角度形式获取[Object Object]并且下拉(选择)未设置为默认 0 索引。虽然在编辑的情况下一切正常。我需要为此创建页面初始化模型属性吗?

在此处输入图像描述

编辑ngModelChange没有调用函数doNameChange

零件:

  export class User {
  Id: number;
  Name: string;
  Gender: string;    
}
this.user = {
      Id: 0,
      Name: '',
      Gender: ''
}
doNameChange(event) {
    debugger;
    console.log(event); // logs model value
  }

html:

 <form (ngSubmit)="save(f.value, f.valid)" #f="ngForm" novalidate materialize>      
      <input id="Name" (ngModelChange)="doNameChange($event)" name="Name" #Name="ngModel" type="text" class="validate form-control" required minlength="3" [(ngModel)]="user.Name">
      <select id="Gender"  name="Gender" #Gender="ngModel" class="validate form-control" [(ngModel)]="user.Gender" required>
          <option value="">-- Select Gender --</option>
          <option value="Male">Male</option>
          <option value="Female">Female</option>
        </select>
    </form>
4

1 回答 1

1

在使用响应式表单时,不要使用用于基于模板的表单的 ngModel。如果您需要收听更改:

ngÒnInit(){
   this.employeeForm = this._fb.group({
      EmployeeId: 0,
      Name: ['', [Validators.required, Validators.minLength(3)]],
      Gender: ['', [Validators.required]]  //dropdown
   });
   this.onChanges();
}
...

onChanges(): void {
  this.myForm.valueChanges.subscribe(val => {
        console.log(val);
  });
}

 //or:
onChanges(): void {
  this.myForm.get('name').valueChanges.subscribe(val => {
    console.log(val);
  });
}
于 2018-06-06T06:31:23.450 回答