简体   繁体   中英

Angular - NgIf not working on component: "NG0303: Can't bind to 'ngIf' since it isn't a known property of 'p'."

I'm trying to use a ngif directive on a component, but keep getting the following error on the console: "NG0303: Can't bind to 'ngIf' since it isn't a known property of 'p'.", and the element just doesn't appear. Also, I'm using po-ui framework for some elements.

Here goes the ts and html codes of the component:

cadastro-usuario.component.ts:

import { Component, OnInit } from '@angular/core';
import { PoButtonGroupModule } from '@po-ui/ng-components';
import { PoButtonGroupItem } from '@po-ui/ng-components';

@Component({
  selector: 'app-cadastro-usuario',
  templateUrl: './cadastro-usuario.component.html',
  styleUrls: ['./cadastro-usuario.component.css']
})
export class CadastroUsuarioComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }
  
  buttons: Array<PoButtonGroupItem> = [
    { label: 'Vínculo', action: this.vinculo.bind(this) },
    { label: 'Endereço', action: this.endereco.bind(this) },
    { label: 'Contatos', action: this.contatos.bind(this) },
  ];

  vinculo() {
  alert("vinculo");
  }
  
  endereco() {
  alert("endereco");
  }
  
  contatos() {
  alert("contatos");
  }
}

cadastro-usuario.component.html:

<p style="display:inline-block; margin-left:80px; margin-top:20px; width: 600px;">
    <po-button-group class="po-md-12" [p-buttons]="buttons"> </po-button-group>
</p>

<br>

<p *ngIf="true" style="display:inline-block; margin-left:80px; margin-top:20px; width: 300px;">
    <po-input name="vinculo" p-label="Vínculo"> </po-input>
</p>

I guess this is a possible duplicate nevertheless you should import

CommonModule and BrowserModule

under the module of your component and if you are not sure just add it the app.module.ts file like this:

1 Imports:

import { CommonModule } from '@angular/common';
import { BrowserModule } from '@angular/platform-browser';

2 Under NgModule:

@NgModule({
  imports: [
    CommonModule,
    BrowserModule
...

If you are here and importing the CommonModule and Browser Module solution did not work. Double check the *ngIf statement has the correct camel casing. Having the casing for example as *ngif will throw the referenced error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM