How to two-way bind my own RxJS Subject to an [(ngModel)]?

前端 未结 6 685
一生所求
一生所求 2021-01-31 15:42

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:



        
6条回答
  •  花落未央
    2021-01-31 16:28

    A possible solution is a sublcass of BehaviorSubject:

    class ModelSubject extends BehaviorSubject {
    
        constructor(initialValue: T) {
            super(initialValue);
        }
    
        set model(value: T) {
            this.next(value);
        }
    
        get model(): T {
            return this.value;
        }
    }
    

    Usage:

    Component-Class:

    name = new ModelSubject('');
    

    Component-Template:

    
    

提交回复
热议问题