参考此处的教程:http://blog.thoughtram.io/angular/2016/06/22/model-driven-forms-in-angular-2.html
这就是我在我的应用程序中使用反应形式的方式:
<div class="login">
<form [formGroup]="form" (ngSubmit)="dologin(form.value)">
<div class="form-group">
<label for="username">Username</label>
<input id="username" type="text" class="form-control" name="username" formControlName="username">
<div [hidden]="form.controls['username'].valid || form.controls['username'].pristine"
class="alert alert-danger"> Username is required.</div>
</div>
<div class="form-group">
<label for="password">Password</label>
<input id="password" type="password" class="form-control" name="password" [formControl]="form.controls['password']">
<div [hidden]="form.controls['password'].valid || form.controls['password'].pristine"
class="alert alert-danger"> Password is required.</div>
</div>
<button type="submit" [disabled]="!form.valid" class="btn btn-primary">Login</button>
</form>
</div>
export class LoginComponent implements OnInit {
form:FormGroup;
errorMessage:string;
public isUserAuthenticated = false;
constructor(private router: Router,private authenticationService : AuthenticationService,private httpService:HttpService,private formBuilder:FormBuilder) {
this.form=formBuilder.group({
username:['',Validators.required],
password:['',Validators.required]
});
}
但不知怎的,我收到编译错误如下:
未处理的Promise拒绝:模板解析错误:无法绑定到 'formGroup',因为它不是'form'的已知属性。 (“] [formGroup] =”形式“ (ngSubmit)= “dologin(form.value)” &GT; ] [formControl] = “form.controls [ '密码']” &GT; [错误 - &gt;] 用户名 [错误 - &gt;]
“):LoginComponent @ 4:8没有NgControl的提供者(” 密码 [错误 - &gt;];任务:Promise.then;值: 错误:模板解析错误:(...)错误:模板解析错误:不能 绑定到'formGroup',因为它不是'form'的已知属性。 (“
] [formGroup] = “形式” (ngSubmit)= “dologin(form.value)” &GT; ] [formControl] = “form.controls [ '密码']” &GT; [错误 - &gt;] 用户名 [错误 - &gt;]“):LoginComponent @ 4:8没有NgControl的提供者(” 密码 [错误 - &gt;] http://ws1066.intern.alpha-raid.com:9002/main.bundle.js:17418:19) 在RuntimeCompiler._compileTemplate(http://ws1066.intern.alpha-raid.com:9002/main.bundle.js:41756:51) 在http://ws1066.intern.alpha-raid.com:9002/main.bundle.js:41678:83 at Set.forEach(native) 在编译时(http://ws1066.intern.alpha-raid.com:9002/main.bundle.js:41678:47) 在ZoneDelegate.invoke(http://ws1066.intern.alpha-raid.com:9002/main.bundle.js:96428:28) 在Zone.run(http://ws1066.intern.alpha-raid.com:9002/main.bundle.js:96321:43) 在http://ws1066.intern.alpha-raid.com:9002/main.bundle.js:96687:57 在ZoneDelegate.invokeTask(http://ws1066.intern.alpha-raid.com:9002/main.bundle.js:96461:37) 在Zone.runTask(http://ws1066.intern.alpha-raid.com:9002/main.bundle.js:96361:47)consoleError @ zone.js:355_loop_1 @ zone.js:382drainMicroTaskQueue @ zone.js:386 zone.js:357错误:未捕获(在承诺中):错误:模板解析 错误:(...)consoleError @ zone.js:357_loop_1 @ zone.js:382drainMicroTaskQueue @ zone.js:386
我搜索了NgControl提供商,在那里我看到我想要导入FORM_Directives但是在Angular2.1中他们没有提到任何关于Form指令的内容。
请帮我弄明白,我在这里缺少什么。
这是我的package.json
{
"name": "raidar-frontend2",
"version": "0.0.0",
"license": "MIT",
"angular-cli": {},
"scripts": {
"start": "ng serve --port 9002 --host ws1069.intern.alpha-raid.com",
"lint": "tslint \"src/**/*.ts\"",
"test": "ng test",
"pree2e": "webdriver-manager update",
"e2e": "protractor"
},
"private": true,
"dependencies": {
"@angular/common": "~2.0.0",
"@angular/compiler": "~2.0.0",
"@angular/core": "~2.0.0",
"@angular/forms": "~2.0.0",
"@angular/http": "~2.0.0",
"@angular/material": "^2.0.0-alpha.9-3",
"@angular/platform-browser": "~2.0.0",
"@angular/platform-browser-dynamic": "~2.0.0",
"@angular/router": "~3.0.0",
"@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.9",
"bootstrap": "^3.3.7",
"core-js": "^2.4.1",
"angular2-localstorage": "^0.4.0",
"ng2-bootstrap": "^1.1.14",
"rxjs": "5.0.0-beta.12",
"ts-helpers": "^1.1.1",
"zone.js": "^0.6.23"
},
"devDependencies": {
"@types/jasmine": "^2.2.30",
"@types/node": "^6.0.42",
"angular-cli": "1.0.0-beta.17",
"codelyzer": "~0.0.26",
"jasmine-core": "2.4.1",
"jasmine-spec-reporter": "2.5.0",
"karma": "1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-remap-istanbul": "^0.2.1",
"protractor": "4.0.9",
"ts-node": "1.2.1",
"tslint": "3.13.0",
"typescript": "2.0.2"
}
}