所以我显然是 ng2 的新手,目前很多人也是如此。随着第一个版本的发布,我学到了一些东西。我开始获得一些更“ng2”的思考方式。
但是,像 ngIf
这样简单的事情我无法开始工作。
这是我的观点:
<div *ngIf="testVariable" class="checkbox">
<label>
<input type="checkbox" formControlName="rememberMe" value="remember-me"> Remember me
</label>
</div>
在我的 component.ts 中:
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, FormBuilder, Validators } from '@angular/forms';
...
ngOnInit() {
this.loginForm = this._fb.group({
username: ['', [<any>Validators.required]],
password: ['', [<any>Validators.required]],
rememberMe: []
});
this.testVariable = false;
}
我需要导入额外的东西吗?我已经看到导入 CORE_COMPONENTS
的预发布 ng2 示例,但我找不到最近的示例。
我的模块声明:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { LoginComponent } from './login.component';
@NgModule({
imports: [ ReactiveFormsModule, CommonModule ],
exports: [ LoginComponent ],
declarations: [ LoginComponent ],
providers: [ ],
})
export class LoginModule { }
最佳答案
您必须在应用程序的@NgModule
的imports
声明中添加CommonModule
,如下所示:
@NgModule({
imports : [
CommonModule
]
})
export default class AppModule {}
关于Angular 2 - ngIf - 不是 div 的已知属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39826597/