Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't bind to 'ngModel' since it isn't a known property of 'p-autoComplete'

I have installed PrimeNg. I am using p-autoComplete component but this error occurs now. I checked everywhere but couldn't find anything. Also, all I was just trying to replicate one of offical PrimeNg demo. p-autocomplete component.

1 - I've installed PrimeNg like this.

npm install primeng --save
npm install primeicons --save

2 - I have added the module to my app.component.ts file like this.

import { AutoCompleteModule } from 'primeng/autocomplete';
//other imports
@NgModule({
declarations: [
  AppComponent,
  HomeComponent
],
imports: [
  BrowserModule,
  AutoCompleteModule
],
  providers: [CountryServiceService],
  bootstrap: [AppComponent]
})
export class AppModule { }

3 - using Angular Cli I have created a service and added demo code from primeNg official website.

4 - I have showed my service in my app.component.ts file like this.

    import { CountryServiceService } from './country-service.service';
...
providers: [CountryServiceService],
...

5 - Created a component called home and in the home.component.html I copied and pasted official website deme code.

<h3 class="first">Basic</h3>
<p-autoComplete [(ngModel)]="country"         
[suggestions]="filteredCountriesSingle"         (completeMethod)="filterCountrySingle($event)" field="name" [size]="30"     placeholder="Countries" [minLength]="1"></p-autoComplete>
<span style="margin-left:10px">Country: {{country ? country.name||country : 'none'}}</span>
<h3>Advanced</h3>
<p-autoComplete [(ngModel)]="brand" [suggestions]="filteredBrands" (completeMethod)="filterBrands($event)" [size]="30" [minLength]="1" placeholder="Hint: type 'v' or 'f'" [dropdown]="true">
  <ng-template let-brand pTemplate="item">
    <div class="ui-helper-clearfix" style="border-bottom:1px solid #D5D5D5">
      <img src="assets/showcase/images/demo/car/{{brand}}.png" style="width:32px;display:inline-block;margin:5px 0 2px 5px" />
      <div style="font-size:18px;float:right;margin:10px 10px 0 0">{{brand}}</div>
    </div>
  </ng-template>
</p-autoComplete>
<span style="margin-left:50px">Brand: {{brand||'none'}}</span>
<h3>Multiple</h3>
<span class="ui-fluid">
    <p-autoComplete [(ngModel)]="countries" [suggestions]="filteredCountriesMultiple" (completeMethod)="filterCountryMultiple($event)" styleClass="wid100"
    [minLength]="1" placeholder="Countries" field="name" [multiple]="true">
    </p-autoComplete>
</span>
<ul>
  <li *ngFor="let c of countries">{{c.name}}</li>
</ul>

6 - in the home.component.ts file I copied and pasted official website demo code.

7 - in the app.component.html I added my home component to see if everything works.

So, I did these steps and I am not getting the desired behavior or I cant even see a rendered html page.

When I inspect the page I get the following errorenter image description here

So, WHAT IS IT THAT I AM DOING WRONG. SERIOUSLY!... STUPID PrimeNg

like image 309
Aytunc MATRAC Avatar asked Aug 01 '18 10:08

Aytunc MATRAC


People also ask

Can't bind to ngModel since it isn't a known property of P calendar?

If 'p-calendar' is an Angular component and it has 'ngModel' input, then verify that it is part of this module. 2. If 'p-calendar' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule. schemas' of this component to suppress this message.

Can't bind to since it isn't a known property?

What does "Can't bind to 'x' since it isn't a known property of 'y'" mean? link. This error often means that you haven't declared the directive "x" or haven't imported the NgModule to which "x" belongs. Perhaps you declared "x" in an application submodule but forgot to export it.

Can't bind to ngModel since it isn't a known property?

To fix Can't bind to 'ngModel' since it isn't a known property of 'input' error in Angular applications we have to import FormModule in app. module. ts file. If you are using FormBuilder class to create reactive form we have to import ReactiveFormsModule as well to avoid below error.


1 Answers

You need to import the FormsModule since ngModel is part of it:

import { FormsModule }   from '@angular/forms';
         ^^^^^^^^^^^

import { AutoCompleteModule } from 'primeng/autocomplete';
//other imports
@NgModule({
declarations: [
  AppComponent,
  HomeComponent
],
imports: [
  BrowserModule,
  FormsModule,
  ^^^^^^^^^^^^
  AutoCompleteModule
],
  providers: [CountryServiceService],
  bootstrap: [AppComponent]
})
export class AppModule { }
like image 73
Kim Kern Avatar answered Oct 08 '22 19:10

Kim Kern