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. VS2013 调试MVC源码[MVC5.2.3+MVC4Web项目]

    1.目前MVC源码版本为5.2.3,下回来后用VS2013打开,把System.Web.Mvc项目的版本号改为4.0.0.1 2.在解决方案下建一个MVC4项目,.NET选4.5,修改根目录以及Vie ...

  2. 集群重启后启动ambari-server访问Web页面无法启动集群解决

    集群重启后启动ambari-server访问Web页面无法启动集群解决 使用ambari部署的集群重新启动后,必须手动重启ambari-server和所有集群主机上的ambari-agent. amb ...

  3. 第三百四十九、五十天 how can I 坚持

    昨天是忘写博客了,今天下班才突然意思到,搞框架搞了好晚.今天重新下了个好了. 昨天,把存储过程交给同事写了,啥都不会,又一堆问题,折腾了一天. 今天相对轻松些,不过事情还没完..明天又周五了. 还有昨 ...

  4. 第三百二十四天 how can I 坚持

    下午去打了会篮球,好累,又把android开发环境搭建起来了,明天把天气应用搞起来. 今天老妈打电话说昨晚梦到我小时候了.. 是啊,都这么大了,不能让他们老操心了. 过两天买根鱼竿去钓鱼. 睡觉.

  5. 用 Python 脚本实现对 Linux 服务器的监控

    目前 Linux 下有一些使用 Python 语言编写的 Linux 系统监控工具 比如 inotify-sync(文件系统安全监控软件).glances(资源监控工具)在实际工作中,Linux 系统 ...

  6. Perl初识笔记

    前两天项目中遇到了一个Perl脚本程序,需要读懂该程序,由于以前重来没有用过Perl语言,所以没法搞定.今天抽空把该语言的基础看了一遍,基本上内读懂Perl脚本程序了吧.真是如网上很多分享的经验所说, ...

  7. CodeForces 709B Checkpoints (数学,最短路)

    题意:给定你的坐标,和 n 个点,问你去访问至少n-1个点的最短路是多少. 析:也是一个很简单的题,肯定是访问n-1个啊,那么就考虑从你的位置出发,向左访问和向右访问总共是n-1个,也就是说你必须从1 ...

  8. Delphi中GUID相等检查中经典指针应用

    type PGUID = ^TGUID; TGUID = packed record D1: LongWord; D2: Word; D3: Word; D4: array[0..7] of Byte ...

  9. .java 文件中只能定义一个public class 且与文件名相同

  10. php,Allowed memory size of 8388608 bytes exhausted (tried to allocate 1298358 bytes)

    修改apache上传文件大小限制 PHP上传文件大小限制解决方法: 第一: 在php.ini里面查看如下行: upload_max_filesize = 8M    post_max_size = 1 ...