Angular2 Form
2015-07-21 21:24
513 查看
1 basic usage
1 import formDirectivesimport {formDirectives} from 'angular2/forms';
2 inject directive into View
directives: [formDirectives]
3 config your form template
template: `<form #f="form" (submit)="onSubmit(f.value)"> <input type="text" ng-control="username" /> <input type="text" ng-control="password"/> <button type="submit">submit</button> </form>`,
take care : 1 #f='form' 2 ng-control
after you submit you will get the result like {username:xx,password:xx}
4 all the code
/**
* Created by Administrator on 2015/7/24.
*/
import {Component,View} from 'angular2/angular2';
import {formDirectives} from 'angular2/forms';
@Component({
selector: 'form-test'
})
@View({
template: `<form #f="form" (submit)="onSubmit(f.value)">
<input type="text" ng-control="username" />
<input type="text" ng-control="password"/>
<button type="submit">submit</button>
</form>`,
directives: [formDirectives]})
export class FormTest {
constructor() {
}
onSubmit(formGroup) {
console.log(formGroup);
}
}
2 two-way binding
use [(ng-model)]='xxxx'@View({
template: `<form #f="form" (submit)="onSubmit(f.value)">
<input type="text" ng-control="username" [(ng-model)]="login.userName"/>
<input type="text" ng-control="password" [(ng-model)]="login.password"/>
<button type="submit">submit</button>
</form>`,
directives: [formDirectives]})
3 ng-control-group
@View({
template: `<form #f="form" (submit)="onSubmit(f.value)">
<div ng-control-group="basic">
<input type="text" ng-control="username" [(ng-model)]="login.basic.userName"/>
<input type="text" ng-control="password" [(ng-model)]="login.basic.password"/>
</div>
<div ng-control-group="company">
<input type="text" ng-control="companyname" [(ng-model)]="login.company.companyName"/>
</div>
<button type="submit">submit</button>
</form>`,
directives: [formDirectives]})
use ng-control-group, you will get the result like
this.login = { basic: { userName: 'Jackey', password: '123' }, company: { companyName: '' } }
相关文章推荐
- Angular简单应用剖析
- 工作积累(八)——AngularJS与单选框及多选框的双向动态绑定
- AngularJS(一)——从零开始AngularJS
- 简话Angular 06 Angular自定义指令
- 简话Angular 05 Angular表单验证
- 简话Angular 04 Angular过滤器详解
- Angular一点基础知识
- AngularJS权威教程 笔记(AngularJS是一个很有意思的库,基于函数形参的依赖注入?酷!还有奇怪的$scope和指令)
- Angularjs路由.让人激动的技术.真给前端长脸了.
- AngularJS学习笔记
- AngularJs教程地址
- Angularjs过滤器的开发.
- Angular2 Decorator
- Angular2 How to Repeat a List
- Angular2 inject a Component into a Component
- Angular2 Template Syntax
- 1 Angular2 Set up
- 简话Angular 03 Angular内置表达式大全
- angular directive详解
- 创建 AngularJS 自定义过滤器,带自定义参数