skip to Main Content

I use Angular version 16 and VScode, the code is running properly but I got this error:

can't bind to 'ngModel' since it isn't a known property of 'input'

. I have app.module.ts:

import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HeaderComponent } from './components/header/header.component';
import { ProductsModule } from './modules/products/products.module';
import { SalesModule } from './modules/sales/sales.module';

@NgModule({
  declarations: [AppComponent, HeaderComponent],
  imports: [BrowserModule, AppRoutingModule, ProductsModule, SalesModule],
  bootstrap: [AppComponent]
})
export class AppModule { }

app-routing.module.ts:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { NotFoundComponent } from './components/not-found/not-found.component';

const routes: Routes = [
  { path: '', redirectTo: '/products', pathMatch: 'full' },
  { path: 'sales', loadChildren: () => import('./modules/sales/sales.module').then(m => m.SalesModule) },
  { path: 'products', loadChildren: () => import('./modules/products/products.module').then(m => m.ProductsModule) },
  { path: '**', component: NotFoundComponent }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

and here is sales.modules.ts:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';
import { SalesRoutingModule } from './sales-routing.module';
import { AddSaleComponent } from 'src/app/components/sales/add-sale/add-sale.component';
import { SalesService } from 'src/app/services/sales.service';
import { BrowserModule } from '@angular/platform-browser';

@NgModule({
  declarations: [
    AddSaleComponent
  ],
  imports: [
    BrowserModule,
    CommonModule,
    SalesRoutingModule,
    FormsModule, 
    ReactiveFormsModule
  ],
  providers: [SalesService]
})
export class SalesModule { }

also here is sales-routing.module.ts:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AddSaleComponent } from 'src/app/components/sales/add-sale/add-sale.component';

const routes: Routes = [
  {
    path: '',
    component: AddSaleComponent,
  },
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
})
export class SalesRoutingModule {}

so, in add-sale.component.html , I’ve got an error for a simple ngModel:

Favorite Color: <input type="text" [(ngModel)]="favoriteColor">

and here is add-sale.component.html.ts:

import { Component } from '@angular/core';

@Component({
  selector: 'app-add-sale',
  templateUrl: './add-sale.component.html',
  styleUrls: ['./add-sale.component.scss'],
})
export class AddSaleComponent {
  favoriteColor = '';
  
}

I’ve already checked Can't bind to 'ngModel' since it isn't a known property of 'input' but still got the error. Do you have any suggestions?

UPDATE: I removed PrdocutsModule and SalesModule from appModule:

import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HeaderComponent } from './components/header/header.component';

@NgModule({
  declarations: [AppComponent, HeaderComponent],
  imports: [BrowserModule, AppRoutingModule, FormsModule],
  bootstrap: [AppComponent]
})
export class AppModule { }

but still does not work.

2

Answers


  1. Chosen as BEST ANSWER

    Here is the issue: https://github.com/angular/vscode-ng-language-service/issues/1894

    To resolve the problem , you should update VSCode and then reload Angular Language Service to have the same version (16). Good to know that, they removed ngcc, more information is here: https://itnext.io/angular-16-is-huge-67288a3ff58b


  2. You should not import your lazy modules in the appComponent:

    @NgModule({
      ...
      imports: [BrowserModule, AppRoutingModule, ProductsModule, SalesModule],
      ...
    })
    export class AppModule { }
    

    As doing so will eagerly load them, no more lazily loaded.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search