Angular - Can't bind to 'ngValue' since it isn't a known property of 'mat-option'

拜拜、爱过 提交于 2019-12-30 17:33:20

问题


I'm using angular 5 and I'm getting the console error:

Can't bind to 'ngValue' since it isn't a known property of 'mat-option'

My template looks something like as follows:

  <mat-select placeholder="Select Book" name="patient" [(ngModel)]="selectedBook">
     <mat-option *ngFor="let eachBook of books" [ngValue]="eachBook">{{eachBook.name}}</mat-option>
  </mat-select>

I've imported both MatSelectModule and MatOptionModule.

How can we resolve this?


回答1:


You should use value

[value]="eachBook"



回答2:


The accepted answer is not a solution but a work-around, as value and [ngValue] serve different purposes. value can be used for simple string values, whereas [ngValue] is necessary to support non-string values.

Per the documentation:

If you have imported the FormsModule or the ReactiveFormsModule, this value accessor will be active on any select control that has a form directive. You do not need to add a special selector to activate it.

If you are getting this error, you most likely need to import either FormsModule or ReactiveFormsModule into your app.

For example, in app.module.ts:

import { FormsModule } from '@angular/forms';

// ...

imports: [
    FormsModule,
    ...
]



回答3:


I have met the same issue. The solution for me is to import 'ReactiveFormsModule'. So you can use [ngValue] to bind an object.



来源:https://stackoverflow.com/questions/48782240/angular-cant-bind-to-ngvalue-since-it-isnt-a-known-property-of-mat-option

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!