dqdq dqsss
dqdq dqsss

Reputation: 21

Can't bind to 'ngModel' since it isn't a known property of 'input' error when trying to run Angular app

I have this input field in the html file of my Angular component:

<input [(ngModel)]="bookRequest.title" type="text" class="form-control" id="title" name="title"
                   placeholder="Book title">

However, I get the following error when trying to launch the app:

Can't bind to 'ngModel' since it isn't a known property of 'input'. [plugin angular-compiler]

This is happening despite importing FormsModule in my app.module.ts. Here is the whole file:

import {FormsModule} from '@angular/forms';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { LoginComponent } from './pages/login/login.component';
import { RegisterComponent } from './pages/register/register.component';
import {FormsModule} from '@angular/forms';
import {HTTP_INTERCEPTORS, HttpClient, HttpClientModule} from '@angular/common/http';
import {HttpTokenInterceptor} from './services/interceptor/http-token.interceptor';
import { ActivateAccountComponent } from './pages/activate-account/activate-account.component';
import {CodeInputModule} from 'angular-code-input';



@NgModule({
  declarations: [
    AppComponent,
    LoginComponent,
    RegisterComponent,
    ActivateAccountComponent
  ],
    imports: [
        BrowserModule,
        AppRoutingModule,
        FormsModule,
        HttpClientModule,
        CodeInputModule
    ],
  providers: [
    HttpClient,
    {
      provide: HTTP_INTERCEPTORS,
      useClass: HttpTokenInterceptor,
      multi: true
    },

  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

From what I've seen online, it may have something to do with my app.component.spec.ts file, but I'm not sure what I'm supposed to change. Here is the whole file:

import { TestBed } from '@angular/core/testing';
import { RouterModule } from '@angular/router';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [
        RouterModule.forRoot([])
      ],
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  });

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });

  it(`should have as title 'book-network-ui'`, () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app.title).toEqual('book-network-ui');
  });

  it('should render title', () => {
    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    const compiled = fixture.nativeElement as HTMLElement;
    expect(compiled.querySelector('h1')?.textContent).toContain('Hello, book-network-ui');
  });
});

I haven't marked the AppComponent as standalone. This is the app.component.ts file:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrl: './app.component.scss'
})
export class AppComponent {
  title = 'book-network-ui';
}

If it helps, this is the schematics in my angular.json file:

"schematics": {
        "@schematics/angular:component": {
          "style": "scss",
          "standalone": false
        },
        "@schematics/angular:directive": {
          "standalone": false
        },
        "@schematics/angular:pipe": {
          "standalone": false
        }
      }

I am aware that a similar question has been posted here before, but that seems to be from many years ago and the version of Angular has changed quite a bit since then. Would appreciate any help

Upvotes: 1

Views: 123

Answers (1)

Naren Murali
Naren Murali

Reputation: 56052

You are getting this error, because you need to import the FormsModule into the testbed for ngModel to work, you can do this same method for other errors related to elements not recognized.

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [
        FormsModule, // <- changed here!
        RouterModule.forRoot([])
      ],
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  });

Upvotes: 0

Related Questions