[Angular 2] Validation
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的更多相关文章
- Angular 4+ 修仙之路
Angular 4.x 快速入门 Angular 4 快速入门 涉及 Angular 简介.环境搭建.插件表达式.自定义组件.表单模块.Http 模块等 Angular 4 基础教程 涉及 Angul ...
- ngVerify - 更高效的 angular 表单验证
ngVerify v1.5.0 a easy Angular Form Validation plugin.简洁高效的__angular表单验证插件__ See how powerful it.看看它 ...
- angular validation 使用总结
我由于制作登陆界面,用到了angular-validation,结合ng-cookies,实现记住账户密码的功能.文档是https://github.com/hueitan/angular-valid ...
- [Angular2 Form] Style Validation in Angular 2 Forms
Inputs using Angular 2’s ngModel automatically apply style classes of .ng-validand .ng-invalid each ...
- [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 ...
- [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 ...
- angular : ngModel 内部流程
angular 1.5 beta link NgModelController provides API for the ngModel directive. The controller conta ...
- 创建自定义的 Angular Schematics
本文对 Angular Schematics 进行了介绍,并创建了一个用于创建自定义 Component 的 Schematics ,然后在 Angular 项目中以它为模板演练了通过 Schemat ...
- [转]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 ...
随机推荐
- Devexpress Barmanager设置
一,在bar的属性中有optionbar,可以做一些设置. 其中比较有用的是:1,去掉最右边的箭头:allowquickcustomization 改为false 2,去掉最左边的竖线:drawdra ...
- 五分钟看懂js关键字this
this是js里面很常用的关键字,而灵活的js也赋予了这个关键字无穷的生命力,相信你也有被它糊弄的时候,我总结了一个6字原则,大部分场合都能清醒分辨this到底指向who,跟大家分享一下,欢迎指正. ...
- 如何在search中动态的显示和隐藏tree中的字段
在tree定义 invisible 来自context <field name="country_id" invisible="context.get('invis ...
- windows server 2012 iis8.0部署mvc报错
一开始以为需要在服务器装mvc在很多论坛找过也问了朋友都说需要装mvc,经过两天研究是不需要装mvc的只需要在项目的bin文件夹下放入下面三个dll. 未能加载文件或程序集“System.Web.Ht ...
- C#后台找不到前台html标签
没关系! 只要他在form表单里 , 咱在标签加上一个 runat="server"就可以在后台cs代码里找到他了
- [Forward]Use the SharePoint My Tasks Web Part outside of My Sites
from http://yalla.itgroove.net/2014/04/use-sharepoint-tasks-web-part-outside-sites/ Use the SharePoi ...
- Unity NGUI 3.0.4版本 制作网络版斗地主
Unity NGUI 3.0.4版本 @by 灰太龙 开发环境 Win7旗舰版 Unity 4.2.1f4 本文就写个开门篇,告诉大家怎么用NGUI,第一步导入NGUI 3.0.4版本! 1.启动U ...
- Unity NGUI中Anchor的用法
unity版本 4.5.1 NGUI版本 3.6.5 通过NGUI中的Anchor设置按钮位置. 1.首先用NGUI创建两个按钮,按钮的创建在Hierachy窗口中必须按如下形式: Anchor用于确 ...
- BZOJ 1083 [SCOI2005]繁忙的都市
1083: [SCOI2005]繁忙的都市 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1664 Solved: 1080[Submit][Sta ...
- 1710: [Usaco2007 Open]Cheappal 廉价回文
Description 为 了跟踪所有的牛,农夫JOHN在农场上装了一套自动系统. 他给了每一个头牛一个电子牌号 当牛走过这个系统时,牛的名字将被自动读入. 每一头牛的电子名字是一个长度为M (1 & ...