Define a filed should has validation:

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);
}
}

Form message

<div *ng-if="!myForm.valid"
class="bg-warning">Form is invalid</div>

Field message

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

Field coloring

<div class="form-group" [class.has-error]="!sku.valid && sku.touched">
<label for="skuInput">SKU</label>
<input type="text"
class="form-control"
id="skuInput"
placeholder="SKU"
[ng-form-control]="myForm.controls['sku']">
</div>

Specific validation

<div *ng-if="myForm.hasError('required', 'sku')">
SKU is required
</div>
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"
placeholder="SKU"
[ng-form-control]="myForm.controls['sku']">
</div>
<div *ng-if="!sku.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] Validation的更多相关文章

  1. Angular 4+ 修仙之路

    Angular 4.x 快速入门 Angular 4 快速入门 涉及 Angular 简介.环境搭建.插件表达式.自定义组件.表单模块.Http 模块等 Angular 4 基础教程 涉及 Angul ...

  2. ngVerify - 更高效的 angular 表单验证

    ngVerify v1.5.0 a easy Angular Form Validation plugin.简洁高效的__angular表单验证插件__ See how powerful it.看看它 ...

  3. angular validation 使用总结

    我由于制作登陆界面,用到了angular-validation,结合ng-cookies,实现记住账户密码的功能.文档是https://github.com/hueitan/angular-valid ...

  4. [Angular2 Form] Style Validation in Angular 2 Forms

    Inputs using Angular 2’s ngModel automatically apply style classes of .ng-validand .ng-invalid each ...

  5. [Angular2 Form] Display Validation and Error Messaging in Angular 2

    Angular 2’s ngModel provides error objects for each of the built-in input validators. You can access ...

  6. [Angular2 Form] Create Radio Buttons for Angular 2 Forms

    Using Radio Buttons in Angular 2 requires a basic understanding of forms as well as how their labels ...

  7. angular : ngModel 内部流程

    angular 1.5 beta link NgModelController provides API for the ngModel directive. The controller conta ...

  8. 创建自定义的 Angular Schematics

    本文对 Angular Schematics 进行了介绍,并创建了一个用于创建自定义 Component 的 Schematics ,然后在 Angular 项目中以它为模板演练了通过 Schemat ...

  9. [转]Angular: Hide Navbar Menu from Login page

    本文转自:https://loiane.com/2017/08/angular-hide-navbar-login-page/ In this article we will learn two ap ...

随机推荐

  1. Struts2中使用execAndWait后,在 Action中调用getXXX()方法报告java.lang.NullPointerException异常的原因和解决方法

    使用 Struts2 编写页面,遇到一个要长时间运行的接口,因此增加了一个execAndWait ,结果在 Action 中调用 getContext()的时候报告异常 ActionContext c ...

  2. Windows7 IIS7 无法启动计算机上的服务W3SVC如何修复

    错误提示 启动iis7管理服务器提示:无法启动计算机上的服务W3SVC 启动Windows Process Activation Service服务,报错:6801 指定资源管理器中的事务支持未启动或 ...

  3. Oracle 面试宝典 - General Questions

    转自 http://www.orafaq.com/wiki/Interview_Questions Tell us about yourself/ your background. What are ...

  4. HTML 动态显示系统当前时间

    HTML 动态显示系统当前时间: <div id="time"> <script> document.getElementById('time').inne ...

  5. python内置字符串操作方法

    1.capitalize() S.capitalize()->string 首字母大写,其余字母小写. str='A222aaA' str.capitalize()#首字母大写,其余字母小写. ...

  6. nodebb在阿里云主机部署过程

    1.在centos上安装nodejswget http://nodejs.org/dist/v0.8.9/node-v0.8.9.tar.gztar zxvf node-v0.8.9.tar.gzcd ...

  7. bzoj3583: 杰杰的女性朋友 && 4362: Graph

    Description 给出一张n个点的有向图G(V,E).对于任意两个点u,v(u可以等于v),u向v的连边数为: ∑OUT(u,i) * IN(v,i),其中1<=i<=K 其中k和数 ...

  8. Ruby自学笔记(三)— 方法Method

    Ruby做为面向对象语言,肯定要对对象进行相关的操作,这时候就涉及到方法了. 调用方法 - 对象.方法名(实参1,实参2,...,实参n) 方法的分类: 1. 实例方法:顾名思义,就是由实例来调用的方 ...

  9. Agri-Net

    poj1258:http://poj.org/problem?id=1258 题意:生成树的模板题.简单题. #include<iostream> #include<cstring& ...

  10. 微控制器(MCU)架构介绍

    微控制器(MicroController)又可简称MCU或μC,也有人称为单芯片微控制器(Single Chip Microcontroller),将ROM.RAM.CPU.I/O集合在同一个芯片中, ...