[Angular2 Form] Use RxJS Streams with Angular 2 Forms
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!";
})
}
}
[Angular2 Form] Use RxJS Streams with Angular 2 Forms的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- angular2 学习笔记 ( Rxjs, Promise, Async/Await 的区别 )
Promise 是 ES 6 Async/Await 是 ES 7 Rxjs 是一个 js 库 在使用 angular 时,你会经常看见这 3 个东西. 它们都和异步编程有关,有些情况下你会觉得用它们 ...
- [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 ...
- [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] Group Inputs in Angular 2 Forms with ngModelGroup
The ngModelGroup directive allows you to group together related inputs so that you structure the obj ...
- [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 ...
随机推荐
- Hadoop 2 初探
Hadoop 2.6.0的安装略复杂,在一台既有Hadoop 1又有Hadoop 2的server上,要设置好环境变量,必要时候echo $HADOOP_HOME一下看运行的是哪个版本. Master ...
- 神奇的linux发行版 tiny core linux
首先官网在此 http://tinycorelinux.net/ 真正轻量级 名字里带有“tiny”又带有“core”,想必又是一个所谓的“轻量级”发行版. 轻量级我们见多了,debian号称是轻量级 ...
- HW7.13
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- 【数据结构与算法分析——C语言描述】第一章总结 引论
这一章主要复习了一些数学知识,像指数.对数.模运算.级数公式:还有2种证明方法,归纳假设法和反证法.所幸以前学过,重新拾捡起来也比较轻松. 简要地复习了递归,提出了编写递归例程的四条基本法则: 基准情 ...
- [POJ] #1006# Biorhythms : 最小公倍数/同余问题
一. 题目 Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 127263 Accepted: 403 ...
- 【转】webgame前台开发总结--虽然是10年的文章,但是也有参考价值
一.webgame整个游戏流程: 1.预加载(打开游戏页面后,显示进度条,主要加载前期的登陆和创建角色资源,创建角色资源的加载可以放到进入创建角色界面的时候加载,因为玩家除了第一次进入游戏,其他时间基 ...
- ajax 新闻栏目
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Unity3D Keynote
[Unity3D Keynote] 1.场景文件扩展名为.unity. 2.up为Y正方向,down为Y负方向,right为X正方向,left为X负方向,forward为Z正方向,back为z负方向. ...
- USB枚举过程(1)
总的过程 ① host检测到device,reset 获取设备描述符 host发获取设备描述符请求 ->setup ->data0 <-ack Divice 返回设备描述符 -> ...
- python windows错误码
在用python删除文件的时候,一直报这个错误,查了 error5的错误是 拒绝访问 在用python删除文件的时候,一直报这个错误,查了 error5的错误是 拒绝访问.那么是删除权限不够?用管理员 ...