angular - ngModel 不显示该值

标签 angular typescript angular-material angular2-template

在 Html 中,它不显示已分配 selectedValueModel 的 ngModel
也尝试过[ngValue],它给我的ngModelChange函数提供了undefined

提前致谢。

foods: Food[] = [
    {value: 'steak-0', viewValue: 'Steak'},
    {value: 'pizza-1', viewValue: 'Pizza'},
    {value: 'tacos-2', viewValue: 'Tacos'}
  ];
selectedValueModel = {value: 'steak-0', viewValue: 'Steak'};
@Output() selectedValueChange = new EventEmitter()

changing(newValue: any) {
    this.selectedValueModel = newValue
    this.selectedValueChange.emit(newValue)
  }

我的 HTML:

<mat-form-field>
    <mat-select placeholder="Favorite food" 
   [(ngModel)]="selectedValueModel.value" 
   (ngModelChange)="changing($event)" name="food">
      <mat-option *ngFor="let food of foods" [value]="food.value">
        {{food.viewValue}}
      </mat-option>
    </mat-select>
  </mat-form-field>

最佳答案

更改:

<mat-form-field>
<mat-select placeholder="Favorite food" 
[(ngModel)]="selectedValueModel.value"
(ngModelChange)="changing($event)" name="food">
  <mat-option *ngFor="let food of foods" [value]="food.value">
    {{food.viewValue}}
  </mat-option>
</mat-select>

至:

<mat-form-field>
<mat-select placeholder="Favorite food" 
[(ngModel)]="selectedValueModel"
(ngModelChange)="changing($event)" name="food">
  <mat-option *ngFor="let food of foods" [value]="food.value">
    {{food.viewValue}}
  </mat-option>
</mat-select>

关于angular - ngModel 不显示该值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54345219/

相关文章:

javascript - Typescript - 为自己的类进行类型定义而不导入它们

reactjs - 基于表单字段的zod条件验证

angularjs - md-select 中的 ng-model 不会更新所选值

Angular 组件 - 封装 BehaviorSubject 的输出

angular - Angular 的 NO_ERRORS_SCHEMA 有问题吗?

angular - 如何将子组件正确导入到延迟加载的 IonicPage 中?

angularjs - 如何使用 Angular Material 触发按钮选择?

javascript - 如何在 javascript/Angular4+ 中用垂直线显示分层垂直数据?

typescript - 为什么我们在 Rxjs 中需要 Tap 运算符

javascript - 如何使用按钮返回 javascript(Angular Material)的最后一个选项卡?