[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 ...
随机推荐
- cmd命令 chcp
chcp是“change code page”的缩写.(关于代码页的相关知识详见:http://www.cnblogs.com/minisculestep/articles/4920992.html)
- DHTML 教程学习进度备忘
书签:跳过:另外跳过的内容有待跟进 __________________ 学习资源:W3School. _________________ 跳过的内容: 1.这个学习进度和前面几个学习进度,只是学习了 ...
- MVC同一页面循环显示数据库记录(答题/投票系统)
) { //int id = 1; list newlist = db.lists.Find(id); //var q = from p in db.lists where p.id==1 selec ...
- docker集成管理工具-shipyard的开发环境搭建笔记
前段时间一直在研究openstack,后来老师告诉我需要用docker容器来搭建hadoop集群,所以就将战场转移到docker上来了,话说docker最近这段时间太火了,但是说实话我觉得应用起来还不 ...
- 无线网WEP的安全测试及防范
650) this.width=650;" border="0" alt="" src="http://img1.51cto.com/att ...
- 如何判断Android设备是手机还是平板?
转自:http://blog.csdn.net/zuolongsnail/article/details/8682950 Android开发需要适配手机和平板,有些需求在实现中就要判断设备是手机还是平 ...
- LightOJ 1370 - Bi-shoe and Phi-shoe (欧拉函数思想)
http://lightoj.com/volume_showproblem.php?problem=1370 Bi-shoe and Phi-shoe Time Limit:2000MS Me ...
- Javascript中的数据类型之旅
虽然Javascript是弱类型语言,但是,它也有自己的几种数据类型,分别是:Number.String.Boolean.Object.Udefined.Null.其中,Object属于复杂数据类型, ...
- oracle学习 十 数据库的语句优化(持续更)
平时关注Oracle数据库的网友都知道,Oracle性能优化保证了Oracle数据库的健壮性.下面就此提出需要注意的两个原则. 原则一:注意WHERE子句中的连接顺序: ORACLE采用自下而上的 ...
- hdoj 5417 Victor and Machine
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5417 水题,一开始题目读错了做了好久,每次停工以后都是重新计时. 需要注意的是,先除后乘注意加括号 # ...