[Angular 2] The form export from NgFormControl
In last post, we need to create an instanse variable:
- 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:
- <div class="form-group" [class.has-error]="!sku.valid && sku.touched">
- <label for="skuInput">SKU</label>
- <input type="text"
- class="form-control"
- id="skuInput"
- #sku = "form"
- placeholder="SKU"
- [ng-form-control]="myForm.controls['sku']">
- </div>
To use it:
- <div *ng-if="!sku.control.valid"
- class="bg-warning">SKU is invalid</div>
Notic, this is only available for the control, not for the from, if we do:
- <div *ng-if="myForm.control.hasError('required', 'sku')">
- SKU is required
- </div>
Will cause error.
Code:
- import {Component, View, FORM_DIRECTIVES, Validators, FormBuilder, NgIf} from 'angular2/angular2';
- @Component({
- selector: 'demo-form-sku'
- })
- @View({
- directives: [FORM_DIRECTIVES, NgIf],
- template: `
- <div>
- <h2>Demo Form: Sku</h2>
- <!-- ngForm is attched to the form, and #f="form" form is also come from ngForm-->
- <form [ng-form-model]="myForm"
- (submit)="onSubmit(myForm.value)">
- <div class="form-group" [class.has-error]="!sku.valid && sku.touched">
- <label for="skuInput">SKU</label>
- <input type="text"
- class="form-control"
- id="skuInput"
- #sku = "form"
- placeholder="SKU"
- [ng-form-control]="myForm.controls['sku']">
- </div>
- <div *ng-if="!sku.control.valid"
- class="bg-warning">SKU is invalid</div>
- <button type="submit" class="btn btn-default">Submit
- </button>
- <div *ng-if="myForm.hasError('required', 'sku')">
- SKU is required
- </div>
- </form>
- <div *ng-if="!myForm.valid"
- class="bg-warning">Form is invalid</div>
- </div>
- `
- })
- export class DemoFormSku {
- myForm: ControlGroup;
- //sku: AbstractControl;
- 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的更多相关文章
- Angular:Reactive Form的使用方法和自定义验证器
本文将介绍Angular(Angular2+)中Reactive Form的有关内容,包括: Reactive Form创建方法 如何使用验证 自定义验证器 下面开始进入正文! Reactive Fo ...
- [Angular] Custom directive Form validator
Create a directive to check no special characters allowed: import {Directive, forwardRef} from '@ang ...
- Angular写一个Form组件-TagInput
前端开发少不了和表单打交道; Angular中, 提供了强大的表单的支持, 响应式表单(Reactive Form) 和 模板驱动的表单(Template-driven Form) 的双向数据流给我们 ...
- angular $http 与form表单的select-->refine
<!DOCTYPE html> <html ng-app="a2_15"> <head> <meta http-equiv="C ...
- angular $http 与form表单的select
产品线 产品 版本 代码是联动关系 ng-model 绑定数据 设置默认值 ng-options 填充option ng-change 选项变化时的操作截图如下: html <!DOCTYPE ...
- Angular之【form提交问题】
前端页面是这样: <form class="form-horizontal" role="form" name="LoginForm" ...
- 使用angular.js获取form表单中的信息
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [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 ...
- [Angular Form] ngModel and ngModelChange
When using Radio button for Tamplate driven form, we want to change to the value change and preform ...
随机推荐
- jQuery备忘录--私家版
最近在看jQuery,总是看过了忘,不知道该怎么办?准备开启洗脑模式,日常念一念,紧箍咒加身. 1.jQuery方法第一步:ready=>加载html的骨架.而onload=>整个页面加载 ...
- Chrome 中的彩蛋——T-Rex
今天,从网页上看到chrome的T-Rex的彩蛋,眨眼间完了10分钟.分享出来,只是好玩. 当 Chrome 无法连接到互联网时,或者上着网突然掉线,刷新页面时报错,我们都会看到T-Rex的身影,没错 ...
- SharePoint 2013 更新多个用户字段(Person or Group)
有时我们需要更新一个用户到Person or Group类型的字段, 当然这个属性允许设置多个用户, 要如何才能添加新的用户到该字段,同时还不影响原始存在的值. 这里我们需要了解 SPFieldUse ...
- [151116 记录] 使用Python3.5爬取豆瓣电影Top250
这一段时间,一直在折腾Python爬虫.已有的文件记录显示,折腾爬虫大概个把月了吧.但是断断续续,一会儿鼓捣python.一会学习sql儿.一会调试OpenCV,结果什么都没学好.前几天,终于耐下心来 ...
- BZOJ 1070 修车
Description 同一时刻有\(N\)位车主带着他们的爱车来到了汽车维修中心.维修中心共有\(M\)位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的.现在需要安排这\(M\)位技术 ...
- Polyfills
填充物..github上比较全的列表 IE总是有填不完的坑,在此总结一下,可能暂时都是一知半解,错了我会慢慢改的. 首先html5标签需要填充一些class,可以用html5.js处理,或者html5 ...
- XE5 Android 开发数据访问server端
http://www.cnblogs.com/key-ok/p/3326064.html http://www.cnblogs.com/key-ok/p/3326055.html http://www ...
- [置顶] export命令-linux
export 命令 功能说明: 设置或显示环境变量. 语 法: export [-fnp][变量名称]=[变量设置值] 补充说明: 在shell中执行程序时,shell会提供一组环境变量. expor ...
- Unity 通过Animation实现控件位置的转换
Unity版本:4.5.1 NGUI版本:3.6.5 参考链接:http://blog.csdn.net/unity3d_xyz/article/details/23035521,作者:CSDN in ...
- Code (组合数)
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7184 Accepted: 3353 Description Trans ...