'Angular Can't bind to 'ngModel' With FormsModule already imported
Note : I'm not a front end dev, even if I do a bit of it. If anything you see can be improved, do not hesitate to share ;)
Strange behavior here, I can't find out why. I have the famous "Can't bind to 'ngModel' since it isn't a known property of 'input'." in the console. But "FormsModule" is imported in the corresponding @Module, which contains the component on the declarations array. So I can't find a solutions on the already answered questions (or if you find, tell me how ;) ). The module :
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { TranslateModule } from '@ngx-translate/core';
import { UserPreferencesComponent } from './user-preferences/user-preferences.component';
@NgModule({
declarations: [
UserPreferencesComponent
],
imports: [
CommonModule,
FormsModule,
TranslateModule
]
})
export class UserPreferencesModule { }
The component
import { Component, OnInit } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { UserPreferences } from '../../entities/UserPreferences';
import { UserPreferencesService } from '../../services/user-preferences.service';
@Component({
selector: 'acm-user-preferences',
templateUrl: './user-preferences.component.html',
styleUrls: ['./user-preferences.component.scss']
})
export class UserPreferencesComponent implements OnInit {
public userPrefs: UserPreferences;
public foo: number;
constructor(private translateService: TranslateService, private userPreferencesService: UserPreferencesService) { }
public ngOnInit(): void {
this.userPreferencesService.getUserPreferences('default').subscribe(userPref => {
this.userPrefs = userPref;
});
}
}
The html template
<h1>Preferences</h1>>
<input [(ngModel)]="foo">
The error in the console
Error: src/app/user-preferences/user-preferences/user-preferences.component.html:13:8 - error NG8002: Can't bind to 'ngModel' since it isn't a known property of 'input'.
13 <input [(ngModel)]="foo">
~~~~~~~~~~~~~~~~~
src/app/user-preferences/user-preferences/user-preferences.component.ts:8:16
8 templateUrl: './user-preferences.component.html',
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component UserPreferencesComponent.
package.json
{
"name": "X",
"version": "0.0.0",
"license": "UNLICENSED",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build --prod",
"test": "ng test",
"lint": "ng lint && npx htmlhint \"**/*.html\" && npx stylelint \"**/*.css\" \"**/*.scss\"",
"e2e": "ng e2e",
"preinstall": "npx npm-force-resolutions"
},
"private": true,
"dependencies": {
"@angular/animations": "^13.1.3",
"@angular/cdk": "^13.1.3",
"@angular/common": "^13.1.3",
"@angular/compiler": "^13.1.3",
"@angular/core": "^13.1.3",
"@angular/forms": "^13.1.3",
"@angular/platform-browser": "^13.1.3",
"@angular/platform-browser-dynamic": "^13.1.3",
"@angular/router": "^13.1.3",
"@ncstate/sat-popover": "^8.0.1",
"@ngx-translate/core": "^14.0.0",
"@ngx-translate/http-loader": "^7.0.0",
"angular-mydatepicker": "^0.11.5",
"angular-spinner": "^1.0.1",
"chart.js": "^3.7.0",
"core-js": "^3.20.3",
"ng2-charts": "^3.0.8",
"ng2-date-picker": "^13.1.1",
"ngx-spinner": "^13.0.0",
"rxjs": "^7.5.2",
"zone.js": "^0.11.4"
},
"devDependencies": {
"@angular-devkit/build-angular": "^13.1.4",
"@angular-eslint/builder": "^13.0.1",
"@angular-eslint/eslint-plugin": "^13.0.1",
"@angular-eslint/eslint-plugin-template": "^13.0.1",
"@angular-eslint/schematics": "^13.0.1",
"@angular-eslint/template-parser": "^13.0.1",
"@angular/cli": "^13.1.4",
"@angular/compiler-cli": "^13.1.3",
"@angular/language-service": "^13.1.3",
"@angular/localize": "^13.1.3",
"@angular/service-worker": "^13.1.3",
"@types/jasmine": "^3.10.3",
"@types/jasminewd2": "^2.0.10",
"@types/node": "^17.0.10",
"@typescript-eslint/eslint-plugin": "^5.10.0",
"@typescript-eslint/parser": "^5.10.0",
"eslint": "^8.7.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-jsdoc": "^37.6.1",
"eslint-plugin-prefer-arrow": "1.2.3",
"eslint-plugin-unused-imports": "^2.0.0",
"htmlhint": "^1.1.0",
"jasmine-core": "^4.0.0",
"jasmine-spec-reporter": "~7.0.0",
"karma": "^6.3.11",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage-istanbul-reporter": "^3.0.3",
"karma-firefox-launcher": "^2.1.2",
"karma-jasmine": "~4.0.1",
"karma-jasmine-html-reporter": "^1.7.0",
"ng-packagr": "^13.1.3",
"postcss": "^8.4.5",
"postcss-scss": "^4.0.3",
"protractor": "~7.0.0",
"set-value": "^4.1.0",
"stylelint": "^14.2.0",
"stylelint-config-standard": "^24.0.0",
"stylelint-config-standard-scss": "^3.0.0",
"tailwindcss": "^3.0.15",
"ts-node": "^10.4.0",
"typescript": "^4.5.5"
},
"resolutions": {
"set-value": "^4.1.0"
}
}
At this point I'm lost, I don't know where I have done something wrong....
Solution 1:[1]
After some times, I finally found it : I did not imported the UserPreferencesModule in the AppModule... So simple, yet so many times... Have a nice day every one !
Solution 2:[2]
Please import the below code in your "UserPreferencesModule" module file
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
and below code add in imports array
imports : [ FormsModule, ReactiveFormsModule ]
if you don't want to create a <form>
then add this code in your input
[(ngModel)]="foo" [ngModelOptions]="{standalone: true}"
Hope, it might be helpful.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | MLem |
Solution 2 | Nikhil Makwana |