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

最新文章

  1. kkjcre1p: unable to spawn jobq slave process, slot 0, error 1089(Linux x86_64)补丁
  2. SQL数据库添加新账号,只操作指定数据库
  3. sh.status()
  4. vsftpd配置参数详细整理
  5. 正则表达式 ——python 基础
  6. 拒绝了对对象 &#39;**&#39; (数据库 &#39;db&#39;,架构 &#39;dbo&#39;)的 SELECT 权限
  7. CSS 文本格式
  8. web项目学习之基于注解的Controller
  9. prototype/constructor/__proto__之prototype简单应用
  10. 批处理就是windows的杰作啊
  11. Spring Boot ConfigurationProperties validate
  12. 【XSY2887】【GDOI2018】小学生图论题 分治FFT 多项式exp
  13. 巨杉数据库完成C轮数千万美元融资
  14. 老男孩python学习自修第七天【包与模块】
  15. Haproxy Nginx cluster构建
  16. 使用httpClient发送post请求连接restful接口
  17. python中stack在实际中的简单应用之进制转换
  18. 微信h5支付“网站域名ICP备案主体与商户号主体不一致”的解决方法,H5微信支付 授权函下载
  19. New users can not log on Win8
  20. java代码中实现android背景选择的selector-StateListDrawable的应用

热门文章

  1. 八皇后问题 --- 递归解法 --- java代码
  2. Spark connect to Database
  3. vs2013下自动注释的运用
  4. PHPCMS V9实现硬件地址MAC绑定访问技术实现
  5. 非对称认证方式 可以用在 asp.net webapi 的安全机制里面
  6. 第二百零九天 how can I 坚持
  7. Unity3D Keynote
  8. #用Python直接写UTF-8文本文件
  9. C#中的DllImport
  10. How to check “hasRole” in Java Code with Spring Security?