<!DOCTYPE html>
<html>
<head>
<title>Angular 2 Form Model</title>
<script src="https://npmcdn.com/systemjs@0.19.6/dist/system.src.js"></script>
<script src="https://code.angularjs.org/tools/typescript.js"></script>
<script src="system.config.js"></script>
</head>
<body>
<login-form>Loading...</login-form>
<script>
System.import('app');
</script>
</body>
</html>
import { bootstrap } from '@angular/platform-browser-dynamic';
import { LoginForm } from './login-form.component.ts';
import { disableDeprecatedForms, provideForms } from '@angular/forms';
bootstrap(LoginForm,
[
disableDeprecatedForms(),
provideForms()
]);
var angularVersion = '2.0.0-rc.4';
var formsVersion = '0.2.0'
System.config({
baseUrl: '/',
paths: {
'npmcdn:*': 'https://npmcdn.com/*'
}
});
System.config({
transpiler: 'typescript',
typescriptOptions: { emitDecoratorMetadata: true },
meta: {
'*': {
deps: [ 'zone.js', 'reflect-metadata' ]
}
}
});
System.config({
packageConfigPaths: [
"npmcdn:@*/*/package.json"
],
map: {
'@angular/core': 'npmcdn:@angular/core@'+angularVersion,
'@angular/compiler': 'npmcdn:@angular/compiler@'+angularVersion,
'@angular/common': 'npmcdn:@angular/common@'+angularVersion,
'@angular/platform-browser': 'npmcdn:@angular/platform-browser@'+angularVersion,
'@angular/platform-browser-dynamic': 'npmcdn:@angular/platform-browser-dynamic@'+angularVersion,
'@angular/http': 'npmcdn:@angular/http@'+angularVersion,
'@angular/forms': 'npmcdn:@angular/forms@'+formsVersion,
'immutable': 'npmcdn:immutable@3.8.1',
'rxjs': 'npmcdn:rxjs@5.0.0-beta.6',
'zone.js': 'npmcdn:zone.js@0.6.12',
'reflect-metadata': 'npmcdn:reflect-metadata@0.1.3',
"crypto": "@empty"
},
packages: {
'app': {
defaultExtension: 'ts',
main: './index.ts'
}
}
});
.ng-invalid.ng-dirty {
border-left: 5px solid red;
}
import {Component} from '@angular/core';
import {
REACTIVE_FORM_DIRECTIVES,
FormBuilder,
FormControl,
Validators
} from '@angular/forms';
@Component({
selector: 'login-form',
templateUrl: 'app/login-form.component.html',
directives: [REACTIVE_FORM_DIRECTIVES]
})
export class LoginForm {
loginForm: FormGroup;
username: FormControl;
password: FormControl;
constructor (builder: FormBuilder) {
this.username = new FormControl('', [
Validators.required,
Validators.minLength(5)
]);
this.password = new FormControl('', [Validators.required]);
this.loginForm = builder.group({
username: this.username,
password: this.password
});
}
login () {
console.log(this.loginForm.value);
// Attempt Logging in...
}
}
<form [formGroup]="loginForm" (ngSubmit)="login()">
<div>
<label for="username">username</label>
<input type="text" name="username" id="username" [formControl]="username">
<div [hidden]="username.valid || username.untouched">
<div>The following problems have been found with the username:
</div>
<div [hidden]="!username.hasError('minlength')">Username can not be shorter than 5 characters.</div>
<div [hidden]="!username.hasError('required')">Username is required.</div>
</div>
</div>
<div >
<label for="password">password</label>
<input type="password" name="password" id="password" [formControl]="password">
<div [hidden]="password.valid || password.untouched">
<div>The following problems have been found with the password:</div>
<div [hidden]="!password.hasError('required')">The password is required.</div>
</div>
</div>
<button type="submit" [disabled]="!loginForm.valid">Log In</button>
</form>