In last post, we need to create an instanse variable:

  1. sku: AbstructControl;

We can get rid of this by getting the exported form from NgFormControl, what we get is a directive, to get control, we need to call .control:

  1. <div class="form-group" [class.has-error]="!sku.valid && sku.touched">
  2. <label for="skuInput">SKU</label>
  3. <input type="text"
  4. class="form-control"
  5. id="skuInput"
  6. #sku = "form"
  7. placeholder="SKU"
  8. [ng-form-control]="myForm.controls['sku']">
  9. </div>

  

To use it:

  1. <div *ng-if="!sku.control.valid"
  2. class="bg-warning">SKU is invalid</div>

Notic, this is only available for the control, not for the from, if we do:

  1. <div *ng-if="myForm.control.hasError('required', 'sku')">
  2. SKU is required
  3. </div>

Will cause error.

Code:

  1. import {Component, View, FORM_DIRECTIVES, Validators, FormBuilder, NgIf} from 'angular2/angular2';
  2.  
  3. @Component({
  4. selector: 'demo-form-sku'
  5. })
  6. @View({
  7. directives: [FORM_DIRECTIVES, NgIf],
  8. template: `
  9. <div>
  10. <h2>Demo Form: Sku</h2>
  11. <!-- ngForm is attched to the form, and #f="form" form is also come from ngForm-->
  12. <form [ng-form-model]="myForm"
  13. (submit)="onSubmit(myForm.value)">
  14. <div class="form-group" [class.has-error]="!sku.valid && sku.touched">
  15. <label for="skuInput">SKU</label>
  16. <input type="text"
  17. class="form-control"
  18. id="skuInput"
  19. #sku = "form"
  20. placeholder="SKU"
  21. [ng-form-control]="myForm.controls['sku']">
  22. </div>
  23. <div *ng-if="!sku.control.valid"
  24. class="bg-warning">SKU is invalid</div>
  25. <button type="submit" class="btn btn-default">Submit
  26. </button>
  27. <div *ng-if="myForm.hasError('required', 'sku')">
  28. SKU is required
  29. </div>
  30.  
  31. </form>
  32. <div *ng-if="!myForm.valid"
  33. class="bg-warning">Form is invalid</div>
  34. </div>
  35. `
  36. })
  37.  
  38. export class DemoFormSku {
  39. myForm: ControlGroup;
  40.  
  41. //sku: AbstractControl;
  1. constructor(fb:FormBuilder) { this.myForm = fb.group({ "sku": ["", Validators.required] }); this.sku = this.myForm.controls['sku']; } onSubmit(value){ console.log(value); } }

[Angular 2] The form export from NgFormControl的更多相关文章

  1. Angular:Reactive Form的使用方法和自定义验证器

    本文将介绍Angular(Angular2+)中Reactive Form的有关内容,包括: Reactive Form创建方法 如何使用验证 自定义验证器 下面开始进入正文! Reactive Fo ...

  2. [Angular] Custom directive Form validator

    Create a directive to check no special characters allowed: import {Directive, forwardRef} from '@ang ...

  3. Angular写一个Form组件-TagInput

    前端开发少不了和表单打交道; Angular中, 提供了强大的表单的支持, 响应式表单(Reactive Form) 和 模板驱动的表单(Template-driven Form) 的双向数据流给我们 ...

  4. angular $http 与form表单的select-->refine

    <!DOCTYPE html> <html ng-app="a2_15"> <head> <meta http-equiv="C ...

  5. angular $http 与form表单的select

    产品线 产品 版本 代码是联动关系 ng-model 绑定数据 设置默认值 ng-options 填充option ng-change 选项变化时的操作截图如下: html <!DOCTYPE ...

  6. Angular之【form提交问题】

    前端页面是这样: <form class="form-horizontal" role="form" name="LoginForm" ...

  7. 使用angular.js获取form表单中的信息

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. [Angular 2] A Simple Form in Angular 2

    When you create a Form in Angular 2, you can easily get all the values from the Form using ControlGr ...

  9. [Angular Form] ngModel and ngModelChange

    When using Radio button for Tamplate driven form, we want to change to the value change and preform ...

随机推荐

  1. jQuery备忘录--私家版

    最近在看jQuery,总是看过了忘,不知道该怎么办?准备开启洗脑模式,日常念一念,紧箍咒加身. 1.jQuery方法第一步:ready=>加载html的骨架.而onload=>整个页面加载 ...

  2. Chrome 中的彩蛋——T-Rex

    今天,从网页上看到chrome的T-Rex的彩蛋,眨眼间完了10分钟.分享出来,只是好玩. 当 Chrome 无法连接到互联网时,或者上着网突然掉线,刷新页面时报错,我们都会看到T-Rex的身影,没错 ...

  3. SharePoint 2013 更新多个用户字段(Person or Group)

    有时我们需要更新一个用户到Person or Group类型的字段, 当然这个属性允许设置多个用户, 要如何才能添加新的用户到该字段,同时还不影响原始存在的值. 这里我们需要了解 SPFieldUse ...

  4. [151116 记录] 使用Python3.5爬取豆瓣电影Top250

    这一段时间,一直在折腾Python爬虫.已有的文件记录显示,折腾爬虫大概个把月了吧.但是断断续续,一会儿鼓捣python.一会学习sql儿.一会调试OpenCV,结果什么都没学好.前几天,终于耐下心来 ...

  5. BZOJ 1070 修车

    Description 同一时刻有\(N\)位车主带着他们的爱车来到了汽车维修中心.维修中心共有\(M\)位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的.现在需要安排这\(M\)位技术 ...

  6. Polyfills

    填充物..github上比较全的列表 IE总是有填不完的坑,在此总结一下,可能暂时都是一知半解,错了我会慢慢改的. 首先html5标签需要填充一些class,可以用html5.js处理,或者html5 ...

  7. XE5 Android 开发数据访问server端

    http://www.cnblogs.com/key-ok/p/3326064.html http://www.cnblogs.com/key-ok/p/3326055.html http://www ...

  8. [置顶] export命令-linux

    export 命令 功能说明: 设置或显示环境变量. 语 法: export [-fnp][变量名称]=[变量设置值] 补充说明: 在shell中执行程序时,shell会提供一组环境变量. expor ...

  9. Unity 通过Animation实现控件位置的转换

    Unity版本:4.5.1 NGUI版本:3.6.5 参考链接:http://blog.csdn.net/unity3d_xyz/article/details/23035521,作者:CSDN in ...

  10. Code (组合数)

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7184   Accepted: 3353 Description Trans ...