Angular 2 forms provide RxJS streams for you to work with the data and validity as it flows out of the forms. These streams allow you handle complex scenarios and asynchronous scenarios with relative ease. This example shows you how to log out the values of the form when the form is valid.

  <!--

    Learn @ViewChld()
valueChanges: show the value,
statusChanges: show VALIDE or INVALIDE,
Observable.combineLatest -->
<form #myForm3="ngForm" name="myForm3">
<input type="text" #techRef="ngModel" ngModel required placeholder="Type Angular2..." name="tech" pattern="Angular2">
<span *ngIf="techRef.valid" class="success-message">{{answer}}</span>
</form>
<div class="error-messages" *ngIf="!myForm3.valid">
<span class="error-message" *ngIf="techRef.errors?.pattern">{{techRef.errors?.pattern.requiredPattern}} only</span>
<span class="error-message" *ngIf="techRef.errors?.required">Requried</span>
</div>
<pre>
Input: {{techRef.errors | json}}
</pre>
import {Component, OnInit, ViewChild} from '@angular/core';
import {Observable} from 'rxjs'; @Component({
selector: 'app-message',
templateUrl: './message.component.html',
styleUrls: ['./message.component.css']
})
export class MessageComponent implements OnInit { @ViewChild('myForm3') form; message = "Hello";
answer: string; constructor() {
} ngOnInit() {
} onSubmit(formValue) {
console.log("formValue", JSON.stringify(formValue, null, ))
} ngAfterViewInit() {
this.form.valueChanges
.subscribe((res) => console.table(res)); this.form.statusChanges
.subscribe((res) => console.log(res)); Observable
.combineLatest(
this.form.valueChanges,
this.form.statusChanges,
(value, status) => ({value, status})
)
.filter( ({status}) => {
return status === "VALID";
})
.subscribe( val => {
this.answer = "You are right!";
}) }
}

Github

[Angular2 Form] Use RxJS Streams with Angular 2 Forms的更多相关文章

  1. [Angular2 Form] Build Select Dropdowns for Angular 2 Forms

    Select Dropdowns in Angular 2 a built with select and option elements. You use *ngFor to loop throug ...

  2. [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 ...

  3. [Angular2 Form] Create and Submit an Angular 2 Form using ngForm

    Forms in Angular 2 are essentially wrappers around inputs that group the input values together into ...

  4. [Angular2 Form] Angular 2 Template Driven Form Custom Validator

    In this tutorial we are going to learn how we can also implement custom form field validation in Ang ...

  5. angular2 学习笔记 ( Rxjs, Promise, Async/Await 的区别 )

    Promise 是 ES 6 Async/Await 是 ES 7 Rxjs 是一个 js 库 在使用 angular 时,你会经常看见这 3 个东西. 它们都和异步编程有关,有些情况下你会觉得用它们 ...

  6. [Angular2 Form] Understand the Angular 2 States of Inputs: Pristine and Untouched

    Angular 2’s ngModel exposes more than just validity, it even gives you the states of whether the inp ...

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

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

  8. [Angular2 Form] Group Inputs in Angular 2 Forms with ngModelGroup

    The ngModelGroup directive allows you to group together related inputs so that you structure the obj ...

  9. [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 ...

随机推荐

  1. extern "c" 的作用

    作用:实现C和C++混合编程. 原理:C和C++编译器编译之后,函数名会编译成不同的名字,链接阶段名字查找会找不到目标,后面实例中会详解. 用法:①.c文件中定义的函数,.cpp文件要调用时,该.cp ...

  2. hadoop的simple认证

    目前Hadoop的稳定版本为1.2.1,我们的实验就在hadoop-1.2.1上进行 Hadoop 版本:1.2.1 OS 版本: Centos6.4 环境配置 机器名 Ip地址 功能 用户 Hado ...

  3. AC多模式匹配算法

    建议:学习ac算法最好的途径是看论文pdf_Efficient_String_Matching_An_Aid_to_Biblio 一.一般的搜索算法 keyword: { he, she, his, ...

  4. Compiling Xen-4.4 From Source And Installing It On Ubuntu Server (Amd-64)

    First of all, you should install a clean Ubuntu Server (Amd-64) on your server. (Version 14.04 is st ...

  5. 应用引擎BAE3.0(转)

    add by zhj: 其实我主要是想看看基于docker的PaaS的特性. 原文:http://developer.baidu.com/wiki/index.php?title=docs/cplat ...

  6. 游戏模块分析总结(2)之UI、操作篇

    转自:http://www.gameres.com/309812.html 游戏模块分析总结(2)之UI.操作篇 发布者: wuye | 发布时间: 2014-12-12 15:03| 评论数: 0 ...

  7. MYSQL数据库性能调优之八:mysql日志

    MySQL日志 主要包含:错误日志.查询日志.慢查询日志.事务日志.二进制日志.中继日志: 使用 SHOW GLOBAL VARIABLES LIKE '%log%';  查询所有日志配置详情: 一. ...

  8. hdoj 1729 Stone Games(SG函数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1729 看了题目感觉像Nim,但是有范围限制,有点不知道SG函数该怎么写 看了题解,最后才明白该怎么去理 ...

  9. Linux 禁用笔记本触摸板

    1. 查看有什么设备 xinput list 输出: ⎡ Virtual core pointer id=2 [master pointer (3)] ⎜ ↳ Virtual core XTEST p ...

  10. MVVM解决方案的一般结构

    解决方案的结构一般是三个解决方案文件夹,分别是: Models ViewModels Views 当然需要的话可以扩充,如Services.UnitTest等等. 然后每个解决方案文件夹里面包含各自的 ...