也许我错过了什么.我找不到关于Observable及其语法的简单教程.我正在使用Angular,我需要从服务调用一个函数(在组件中定义).我读了solution页.但我不知道如何改变服务中创建的可观察值(也许创建不是最好的方法).
我在解决方案中有一个类似的组件:
@Component({
selector: 'my-component',
...
)}
export class MyComponent {
constructor(myService:MyService) {
myService.condition.subscribe(value => doSomething(value));
}
doSomething(value) {
if (value) // do stuff
else // other stuff
}
}
这是我的服务:
import { Injectable } from '@angular/core';
import { Observable} from 'rxjs/Observable';
@Injectable()
export class MyService {
private condition: Observable<boolean>;
constructor() {
this.condition= new Observable(ob => {ob.next(false); })
// maybe ob.next is not the best solution to assign the value?
}
change() {// how can i change the value of condition to 'true', to call
// the doSomething function in the component??
}
}