<!DOCTYPE html>
<html>
<head>
<!-- We set the base href -->
<script>document.write('<base href="' + document.location + '" />');</script>
<title>Angular 2 Forms Example </title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSS file -->
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >
<link rel="stylesheet" type="text/css" href="style.css">
<!-- IE polyfills, keep the order please -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.33.3/es6-shim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.20/system-polyfills.js"></script>
<!-- Agular 2 -->
<script src="https://code.angularjs.org/2.0.0-beta.7/angular2-polyfills.js"></script>
<script src="https://code.angularjs.org/tools/system.js"></script>
<script src="https://code.angularjs.org/tools/typescript.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.7/Rx.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.7/angular2.dev.js"></script>
<!-- Agular 2 Router -->
<script src="https://code.angularjs.org/2.0.0-beta.7/router.dev.js"></script>
<!-- Config Agular 2 and Typescript -->
<script>
System.config({
transpiler: 'typescript',
typescriptOptions: { emitDecoratorMetadata: true },
packages: {'app': {defaultExtension: 'ts'}}
});
System.import('app/main')
.then(null, console.error.bind(console));
</script>
</head>
<!-- Run the application -->
<body>
<h1>Angular 2 Forms Example </h1>
<my-app>Loading Sample...</my-app>
<div style="margin-top:50px" >
<a target="_blank" href="http://www.angulartypescript.com/angular-2-tutorial/" title="Angular 2 Tutorial">
<img src="http://www.angulartypescript.com/wp-content/uploads/2016/03/learn-more-angular-2.png" alt="Smiley face" height="200" width="500">
</a>
<ul class="nav nav-pills" >
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-tutorial/" title="Angular 2 Home"> Angular 2 Tutorial </a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-introduction/">Angular 2 Introduction</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-architecture/">Angular 2 Architecture</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-annotations/">Angular 2 Annotations</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-getting-started/">Angular 2 Setup</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-hello-world/">Angular 2 Hello World</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-components/">Angular 2 Components</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-template-syntax/">Angular 2 Template Syntax</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-data-binding/">Angular 2 Data Binding</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-forms/">Angular 2 Forms</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-formbuilder-example/">Angular 2 Formbuilder</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-router-example/">Angular 2 Router</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-http-example-typescript/">Angular 2 HTTP</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-services/">Angular 2 Service</a></li>
</ul>
</div>
</body>
</html>
<!--
Copyright 2016 angulartypescript.com. All Rights Reserved.
Everyone can use this source code; don’t forget to indicate the source please:
http://www.angulartypescript.com/
-->
/* Styles go here */
/**
* Created by Tareq Boulakjar. from angulartypescript.com
*/
import {bootstrap} from 'angular2/platform/browser';
import {FormComponent} from "./general.component";
bootstrap(FormComponent);
/*
Copyright 2016 angulartypescript.com. All Rights Reserved.
Everyone can use this source code; don’t forget to indicate the source please:
http://www.angulartypescript.com/
*/
/**
* Created by Tareq Boulakjar. from angulartypescript.com
*/
export class GreenCar {
constructor(
public greenCarId: number,
public greenCarName: string,
public greenCarColor: string,
public greenCarDoors: number
) { }
}
/**
* Created by Tareq Boulakjar. from angulartypescript.com
*/
import {Component} from 'angular2/core';
import {FormExample} from "./angular-typescript-form.component";
@Component({
selector: 'my-app',
template: `
<angular-typescript-form>loading...</angular-typescript-form>
`,
directives: [FormExample]
})
export class FormComponent {
}
/*
Copyright 2016 angulartypescript.com. All Rights Reserved.
Everyone can use this source code; don’t forget to indicate the source please:
http://www.angulartypescript.com/
*/
/**
* Created by Tareq Boulakjar. from angulartypescript.com
*/
import {Component,EventEmitter,Output} from 'angular2/core';
import {CORE_DIRECTIVES,FORM_DIRECTIVES} from 'angular2/common';
import {GreenCar} from "./greencar";
// Our form component
@Component({
selector: 'angular-typescript-form',
templateUrl: 'app/angular-typescript-form.component.html',
directives: [CORE_DIRECTIVES, FORM_DIRECTIVES]
})
export class FormExample {
myCar = new GreenCar(1,'BMW Serie 1','Red',2); // this is our green car instance
//the constructor is a good place for initialization (not big things)
constructor(){
console.log("Form Component Start");
}
submitted = false; //form not submited : default
data: string; //this variable contains our data
//Show data after form submit and set submitted to true
onSubmit(data) {
this.submitted = true;
this.data = JSON.stringify(data, null, 2);
console.log(this.data);
}
}
/*
Copyright 2016 angulartypescript.com. All Rights Reserved.
Everyone can use this source code; don’t forget to indicate the source please:
http://www.angulartypescript.com/
*/
<h2 style="color:#0099FF">Angular 2 Forms</h2>
<form (ngSubmit)="onSubmit(carForm.value)" #carForm="ngForm">
<div class="form-group">
<label class="col-xs-4 control-label" for="greenCarName" >Green car name : </label>
<div class="col-xs-8">
<input type="text" style="width: 300px" class="form-control" required
[(ngModel)]="myCar.greenCarName"
ngControl="greenCarName"
#greenCarName="ngForm"
>
<div [hidden]="greenCarName.valid || greenCarName.pristine" class="alert alert-danger">
The Name of green car is required !
</div>
</div>
</div>
<div class="form-group">
<label class="col-xs-4 control-label" for="greenCarColor" >Green car color : </label>
<div class="col-xs-8">
<input type="text" style="width: 300px" class="form-control" required
[(ngModel)]="myCar.greenCarColor"
ngControl="greenCarColor"
#greenCarColor="ngForm"
>
<div [hidden]="greenCarColor.valid || greenCarColor.pristine" class="alert alert-danger">
The Color of green car is required !
</div>
</div>
</div>
<div class="form-group">
<label class="col-xs-4 control-label" for="greenCarDoors" >Green car doors : </label>
<div class="col-xs-8">
<input type="number" style="width: 300px" class="form-control" required
[(ngModel)]="myCar.greenCarDoors"
ngControl="greenCarDoors"
#greenCarDoors="ngForm"
>
<div [hidden]="greenCarDoors.valid || greenCarDoors.pristine" class="alert">
The number of doors is required !
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
<div class="form-group">
<label class="col-xs-4 control-label" >Submitted Data : </label>
<div class="col-xs-8">
<div [hidden]="!submitted">
<span>from angulartypescript.com</span>
<textarea style="width: 300px" class="form-control" rows="6">{{data}}</textarea>
</div>
<div [hidden]="submitted">
<span>Edit your Data then click Submit to get Data from : angulartypescript.com</span>
</div>
</div>
</div>
</form>
<!--
Copyright 2016 angulartypescript.com. All Rights Reserved.
Everyone can use this source code; don’t forget to indicate the source please:
http://www.angulartypescript.com/
-->