问题
I am working in my Ionic 4 project and I have created a custom component but in that custom component my [(ngModel)]
is not working.
This is my custom-header-component.component.html:
<ion-select [(ngModel)]="languageSelected" (ionChange)='setLanguage()' ngDefaultControl>
<ion-select-option value="en">English</ion-select-option>
<ion-select-option value="ar">Arabic</ion-select-option>
</ion-select>
In this html my [(ngModel)]
is not working because it is not printing any value in the console.
This is my custom-header-component.component.ts:
languageSelected: any;
setLanguage() {
let me=this;
console.log('languageSelected',me.languageSelected);
}
In this ts file, it is not printing any value.
Maybe the problem is that, I have not included the FormsModule
.
This is my Folder:
custom-header-component:
|
-- custom-header-component.component.html
-- custom-header-component.component.scss
-- custom-header-component.component.spec.ts
-- custom-header-component.component.ts
components.module.ts
This is my components.module.ts:
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { CustomHeaderComponentComponent } from './custom-header-component/custom-header-component.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@NgModule({
declarations: [CustomHeaderComponentComponent],
exports: [CustomHeaderComponentComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
imports: [
FormsModule,
ReactiveFormsModule
],
})
export class ComponentsModule{}
Any help is much appreciated.
回答1:
It should be <ion-option
<ion-option value="en">English</ion-option>
STACKBLITZ DEMO
EDIT:
If it's ionic 4 ,your code looks correct. As mentioned in the question if you have not included FormsModule
you need to add it as
import { ReactiveFormsModule } from '@angular/forms';
@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [
BrowserModule,
IonicModule.forRoot(),
AppRoutingModule,
ReactiveFormsModule
],
providers: [
StatusBar,
SplashScreen,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule {}
回答2:
you just need to import FormsModule in app.module.ts
import { FormsModule } from '@angular/forms';
and add ComponentPage inside declarations and entryComponents
You can see the example below
In app.module.ts
import { FormsModule } from '@angular/forms';
import { ComponentPage } from './component/component.page'
@NgModule({
declarations: [
ComponentPage,
],
entryComponents: [
ComponentPage,
],
imports: [FormsModule],
})
回答3:
Try This:
<ion-select ngDefaultControl [(ngModel)]="languageSelected" (ionChange)='setLanguage($event)'>
<ion-select-option value='en' [selected]="language==='en' ? true : null">English</ion-select-option>
<ion-select-option value='ar' [selected]="language==='ar' ? true : null">Arabic</ion-select-option>
</ion-select>
setLanguage($event) {
let me=this;
console.log('Default language:', $event.target.value);
}
In this, You can get the selected value.
来源:https://stackoverflow.com/questions/55683500/my-ngmodel-is-not-working-in-the-custom-component-in-ionic-4