ko.observable
の最も近いアナログはSubject
かさえBehaviorSubject
で、ko.computed
のためにあなたがここにObservable.combineLatest
を使用することが一種のHello Worldの例:
import 'rxjs/add/operator/map';
import {Component} from '@angular/core';
import {Observable, BehaviorSubject} from "rxjs";
@Component({
selector: 'app-root',
template: `<div>
<button (click)="both()">both</button>
<button (click)="first()">first</button>
<button (click)="last()">last</button>
<button (click)="ageNow()">age</button>
<hr />
fullName: {{fullName | async}}
<hr />
age: {{age | async}}
</div>`
})
export class AppComponent {
firstName = new BehaviorSubject(''); // aka this.firstName = ko.observable('')
lastName = new BehaviorSubject('');
age = new BehaviorSubject(0);
fullName = Observable.combineLatest(this.firstName, this.lastName) // aka this.fullName = ko.computed(...)
.do(values => console.log('computed fired'))
.map(values => values.join(' ').trim());
both() {
this.first();
this.last();
}
first() {
this.firstName.next('foo ' + Date.now());
}
last() {
this.lastName.next('bar ' + Date.now());
}
ageNow() {
this.age.next(Date.now());
}
}
そして、おそらくあなたはフォームで作業するようにしたいでしょう。例は次のようなものになります:
import 'rxjs/add/operator/map';
import {Component} from '@angular/core';
import {Observable, BehaviorSubject} from "rxjs";
import {FormGroup, FormControl, FormBuilder} from "@angular/forms";
@Component({
selector: 'app-root',
template: `<form [formGroup]="form">
<input formControlName="firstName" />
<input formControlName="lastName" />
{{fullName | async}}
</form>`
})
export class AppComponent {
form:FormGroup;
firstName = new FormControl('');
lastName = new FormControl('');
fullName = Observable.combineLatest(
this.firstName.valueChanges.startWith(''),
this.lastName.valueChanges.startWith('')
).map(values => values.join(' ').trim());
constructor(private fb:FormBuilder) {
this.form = fb.group({
firstName: this.firstName,
lastName: this.lastName
});
}
}
フォームの例ではFormControlではなく、valueChanges
ストリームでのビルドを監視していることに注意してください。また、初期値も定義しています。
あなたは| async
あなたは常にあなたのストリームを購読することができますテンプレート内の配管と、それらのコンポーネントのプロパティ
出典
2017-01-23 15:03:48
mac
に対処していない場合は、この機能の内容はコンピュータだけであることを確認していない場合、入力の変化の一。 getter関数が何らかの計算集約的な作業(オブジェクトの大きなリストの並べ替えなど)を行っている場合、これは引き続き非効率的です。あなたは提供されたplunkrでこれを見ることができます。 –