angular – 如何双向绑定我自己的RxJS主题为[(ngModel)]?
发布时间:2020-12-17 08:03:26 所属栏目:安全 来源:网络整理
导读:是否有简短的方法将RxJS Subject 或 BehaviorSubject 传递给Angular 2指令进行双向绑定?完成它的漫长道路如下: @Component({ template: ` input type="text" [ngModel]="subject | async" (ngModelChange)="subject.next($event)" / `}) 我希望能够做到这
是否有简短的方法将RxJS
Subject 或
BehaviorSubject 传递给Angular 2指令进行双向绑定?完成它的漫长道路如下:
@Component({ template: ` <input type="text" [ngModel]="subject | async" (ngModelChange)="subject.next($event)" /> ` }) 我希望能够做到这样的事情: @Component({ template: ` <input type="text" [(ngModel)]="subject" /> ` }) 我相信异步管道只是单向的,所以这还不够。 Angular 2是否提供了一种简单易行的方法? Angular 2也使用RxJS,因此我预计会有一些固有的兼容性。 我是否可以创建一个类似ngModel的新指令来实现这一目标?
我能想到的最接近的是使用FormControl:
import { FormControl } from '@angular/forms'; @Component({ template: '<input [formControl]="control">' }) class MyComponent { control = new FormControl(''); constructor(){ this.control.valueChanges.subscribe(()=> console.log('tada')) } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |