如何将我自己的 RxJS Subject 双向绑定到 [(ngModel)]? [英] How to two-way bind my own RxJS Subject to an [(ngModel)]?

查看:23
本文介绍了如何将我自己的 RxJS Subject 双向绑定到 [(ngModel)]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简短而简单的方法来传递 RxJS 主题BehaviorSubject 用于双向绑定的 Angular 2 指令?很长的路要走如下:

Is there a short and simple way to pass an RxJS Subject or BehaviorSubject to an an Angular 2 directive for two-way binding? The long way to do it would be as follows:

@Component({
    template: `
        <input type="text" [ngModel]="subject | async" (ngModelChange)="subject.next($event)" />
    `
})

我希望能够做这样的事情:

I'd like to be able to do something like this:

@Component({
    template: `
        <input type="text" [(ngModel)]="subject" />
    `
})

我相信 async 管道只是一种方式,所以这还不够.Angular 2 是否提供了一种简短而简单的方法来做到这一点?Angular 2 也使用 RxJS,所以我希望有一些内在的兼容性.

I believe the async pipe is only one-way, so that's not enough. Does Angular 2 provide a short and simple way to do this? Angular 2 uses RxJS too, so I expected there to be some inherent compatibility.

我能否创建一个新的 ngModel 类指令来实现这一点?

Could I perhaps create a new ngModel-like directive to make this possible?

推荐答案

正如您在问题中所说,这是一个简单的解决方案.我认为没有什么比你已经提供的更简单了.

This is a simple solution, as you said in your question. I think there's nothing simpler than what you already provided.

<input type="text" 
       [ngModel]="subject | async"
       (ngModelChange)="subject.next($event)" />

这篇关于如何将我自己的 RxJS Subject 双向绑定到 [(ngModel)]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆