无法绑定到 'ngValue',因为它不是 'option' 的已知 属性
Can't bind to 'ngValue' since it isn't a known property of 'option'
我正在尝试在 Angular 5 中实现 select
,但我经常收到这个
我已经尝试过很多 Whosebug 问题,唯一的区别是 - 我的组件在应用程序的另一个模块中,最终注入到主模块中。我也试过在内部模块中注入 FormsModule
。我试过导入 ReactiveFormsModule
但没有成功。
我已将 FormsModule
添加到这样的导入中
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppRoutingModule } from './app-routing.module';
@NgModule({
declarations: [
...CombineComponents
],
imports: [
BrowserModule,
FormsModule,
AppRoutingModule,
HttpClientModule
]
});
这是我的组件标记
<label for="ctn" class="d-inline-block pl-1 semi-bold">Current active number</label>
<select
#selectElem
class="custom-select"
id="ctn"
(change)="onCTNChange(selectElem.value)"
formControlName="state"
>
<option value="" disabled>Choose a state</option>
<option *ngFor="let ctn of availableCTN" [ngValue]="ctn.value">
{{ctn.text}}
</option>
</select>
使用value
:
[value]="ctn.value"
我犯了一个非常愚蠢的错误并陷入了这个问题。
- 而不是使用
[ngValue]="ctn.value"
- 我应该使用
[value]
我在父模块中导入 formsModule
,我应该在子模块中导入它以使 [(ngModel)]
工作
如果我们希望显示默认选择,[value]
应该是 [(value)]
。
所以我的最终组件代码是。
<label for="ctn" class="d-inline-block pl-1 semi-bold">Current active number</label>
<select
#selectCTN
class="custom-select"
id="ctn"
[(ngModel)]="selectedCTN"
(change)="onCTNChange(selectCTN.value)"
>
<option value="" disabled>Choose a state</option>
<option *ngFor="let ctn of availableCTN" [(value)]="ctn.value">
{{ctn.text}}
</option>
</select>
[value] 将在这种情况下起作用。就我而言,我也没有使用 formmodule,只是在标签内使用它,工作正常。
我正在尝试在 Angular 5 中实现 select
,但我经常收到这个
我已经尝试过很多 Whosebug 问题,唯一的区别是 - 我的组件在应用程序的另一个模块中,最终注入到主模块中。我也试过在内部模块中注入 FormsModule
。我试过导入 ReactiveFormsModule
但没有成功。
我已将 FormsModule
添加到这样的导入中
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppRoutingModule } from './app-routing.module';
@NgModule({
declarations: [
...CombineComponents
],
imports: [
BrowserModule,
FormsModule,
AppRoutingModule,
HttpClientModule
]
});
这是我的组件标记
<label for="ctn" class="d-inline-block pl-1 semi-bold">Current active number</label>
<select
#selectElem
class="custom-select"
id="ctn"
(change)="onCTNChange(selectElem.value)"
formControlName="state"
>
<option value="" disabled>Choose a state</option>
<option *ngFor="let ctn of availableCTN" [ngValue]="ctn.value">
{{ctn.text}}
</option>
</select>
使用value
:
[value]="ctn.value"
我犯了一个非常愚蠢的错误并陷入了这个问题。
- 而不是使用
[ngValue]="ctn.value"
- 我应该使用
[value]
我在父模块中导入formsModule
,我应该在子模块中导入它以使[(ngModel)]
工作
如果我们希望显示默认选择, [value]
应该是[(value)]
。
所以我的最终组件代码是。
<label for="ctn" class="d-inline-block pl-1 semi-bold">Current active number</label>
<select
#selectCTN
class="custom-select"
id="ctn"
[(ngModel)]="selectedCTN"
(change)="onCTNChange(selectCTN.value)"
>
<option value="" disabled>Choose a state</option>
<option *ngFor="let ctn of availableCTN" [(value)]="ctn.value">
{{ctn.text}}
</option>
</select>
[value] 将在这种情况下起作用。就我而言,我也没有使用 formmodule,只是在标签内使用它,工作正常。