2016-08-20 66 views
77

Can't bind to 'ngIf' since it isn't a known property of 'div'.不能綁定到 'ngIf',因爲它不是 'DIV' 的已知財產

的元素是<div [ngIf]="isAuth" id="sidebar">

和組件是:

import SessionService from '../session/session.service'; 
import { Component } from '@angular/core'; 

@Component({ 
    providers: [], 
    selector: 'navbar-left', 
    styles: [require('./navbar-left.scss')], 
    template: require('./navbar-left.html'), 
}) 
export default class NavbarLeftComponent { 

    public isAuth: boolean = false; 

    constructor(private sessionService: SessionService) { 
    this.isAuth = sessionService.sessionIsAuth(); 
    } 

} 

不知道我究竟做錯了什麼?這是一個兒童組件。在NGIF的父組件中,也就是App組件。 角RC5

+2

@JuanMendes這個問題在8月20日被問到,你指出的問題是3天前。這個怎麼可能與另一個重複? :D –

+0

此問題的正確答案對於最新版本也是正確的。我並不真正遵循你的邏輯,因爲標題完全不同。無論如何,做你認爲最好的事情:D –

+0

你是對的50%。但是這並不能使問題重複。如果你真的想要有幫助,只需指出你還需要添加模塊作爲導入。 Geez,你必須在派對上玩得很開心:D –

回答

103

如果您正在使用RC5然後導入此:

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

,並確保從所提供的部件內置模塊導入CommonModule

@NgModule({ 
    imports: [CommonModule], 
    declarations: [MyComponent] 
    ... 
}) 
class MyComponentModule {} 
+0

請確保您的編譯工作正常,並且您的代碼中有'* ngIf' – AngJobs

+0

錯誤:無法找到名稱「CommonModule」。我導入了「BrowserModule」,但它仍然生成錯誤「ngif不是已知屬性 – Kokodoko

+6

'從'@ angular/common'導入{CommonModule};' –

11

代替[ngIf]你應該使用* ngIf這樣的:

<div *ngIf="isAuth" id="sidebar"> 
114

只爲任何人誰是失蹤了,我也有,我打字ngif而不是ngIf一個問題(注意國會大廈'我')。

+4

純金。謝謝。 – Remotec

+1

'nfIf'在我的情況下,有時...:facepalm: –

+0

現在我覺得很愚蠢...謝謝! –

相關問題