[Angular 2] ng-control & ng-control-group

Control:

Controls encapsulate the field's value, a states such as if it is valid, dirty or has errors.

var nameControl = new Control("Nate");
var name = nameControl.value; // -> Nate

nameControl.errors // -> StringMap<string, any> of errors
nameControl.dirty // -> false
nameControl.valid // -> true

 

ControlGroup:

A way to manage multiple Controls.

var personInfo = new ControlGroup({
    firstName:  new Control("Nate"),
    lastName: new Control("Murray"),
    zip: new Control('90210')
});

personInfo.value; // ->{
   //firstName: "Nate",
   //lastName: "Murray",
   //zip: "90210"  
}

personInfo.errors // -> StringMap<string, any> of errors
personInfo.dirty // -> false
personInfo.valid // -> true

 

 

import {Component, View, FORM_DIRECTIVES} from 'angular2/angular2';

@Component({
    selector: 'demo-form-sku'
})
@View({
    directives: [FORM_DIRECTIVES],
    template: `
       <div>
        <h2>Demo Form: Sku</h2>
        <!-- ngForm is attched to the form, and #f="form" form is also come from ngForm-->
        <form #f = "form"
        (submit)="onSubmit(f.value)">
            <div class="form-group">
                <label for="skuInput">SKU</label>
                <input type="text"
                class="form-control"
                id="skuInput"
                placeholder="SKU"
                ng-control="sku">
            </div>

            <button type="submit" class="btn btn-default">
                Submit
            </button>
        </form>
       </div>
    `
})

export class DemoFormSku {
    constructor() {

    }

    onSubmit(value){
        console.log(value);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值