扩展角2 ngModel指令使用观测
问题描述:
角2 ngModel指令与变量和函数类似于扩展角2 ngModel指令使用观测
<input [ngModel]="myVar" (ngModelChange)="myFunc($event)" />
取而代之的变量和函数,我想用BehaviorSubjects代替
<input [ngModel]="mySubject | async" (ngModelChange)="mySubject.next($event)" />
有没有一种安全的方法来扩展ngModel或使用某种宏来减少我的模板中的重复?
<input [myNewNgModel]="mySubject" />
答
有点迟到了,但我相信你有兴趣在此:
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.less']
})
export class HomeComponent implements OnInit {
model: Observable<number[]>;
ngOnInit() {
this.model = this.get();
}
get(): Observable<number[]> {
return Observable.from([[1, 2, 3]]);
}
}
@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {
@Input() numbers;
constructor() { }
ngOnInit() {
}
}
<app-child [numbers]="(model | async)"></app-child>
<!-- child component -->
<h1 *ngFor="let i of numbers">{{i}}</h1>
听起来像是你正在寻找的东西像https://开头github上。 COM /角/角/问题/ 4062。我确信这将会发布到Angular2上,但是只有在发布之后。 –
@GünterZöchbauer很高兴知道还有其他人试图Rx的一切。主要的区别是我试图扩展/重复ngModel with observables,而提案侧重于将事件绑定到observables。 – nwarp
这是最终解决了什么?我很高兴写出一个回应,但目前我很难理解你是否真的想将这些输入用作表单的一部分?如果是这样,你有没有想过听可观察的形式?如果不是的话,你可以给出一些背景知道你如何使用你的BehaviorSubject?它肯定有用例,但是当我们对RxJS不熟悉时,我们往往会过度使用一些主题。 –