In reference to tutorial here : http://blog.thoughtram.io/angular/2016/06/22/model-driven-forms-in-angular-2.html
this is how i am using reactive form in my application :
<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]
});
}
but somehow i am getting compilation errors as below:
Unhandled Promise rejection: Template parse errors: Can't bind to 'formGroup' since it isn't a known property of 'form'. (" ][formGroup]="form" (ngSubmit)="dologin(form.value)"> ][formControl]="form.controls['password']"> [ERROR ->] Username [ERROR ->]
"): LoginComponent@4:8 No provider for NgControl (" Password [ERROR ->] ; Task: Promise.then ; Value: Error: Template parse errors:(…) Error: Template parse errors: Can't bind to 'formGroup' since it isn't a known property of 'form'. ("
][formGroup]="form" (ngSubmit)="dologin(form.value)"> ][formControl]="form.controls['password']"> [ERROR ->] Username [ERROR ->]"): LoginComponent@4:8 No provider for NgControl (" Password [ERROR ->]http://ws1066.intern.alpha-raid.com:9002/main.bundle.js:17418:19) at RuntimeCompiler._compileTemplate (http://ws1066.intern.alpha-raid.com:9002/main.bundle.js:41756:51) at http://ws1066.intern.alpha-raid.com:9002/main.bundle.js:41678:83 at Set.forEach (native) at compile (http://ws1066.intern.alpha-raid.com:9002/main.bundle.js:41678:47) at ZoneDelegate.invoke (http://ws1066.intern.alpha-raid.com:9002/main.bundle.js:96428:28) at Zone.run (http://ws1066.intern.alpha-raid.com:9002/main.bundle.js:96321:43) at http://ws1066.intern.alpha-raid.com:9002/main.bundle.js:96687:57 at ZoneDelegate.invokeTask (http://ws1066.intern.alpha-raid.com:9002/main.bundle.js:96461:37) at 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:357Error: Uncaught (in promise): Error: Template parse errors:(…)consoleError @ zone.js:357_loop_1 @ zone.js:382drainMicroTaskQueue @ zone.js:386
I searched about NgControl providers , there i see that i am suppose to import the FORM_Directives but in Angular2.1 they have not mentioned anything about Form directives.
Please help me to figure out , what am i missing here.
Here is my 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"
}
}