【发布时间】:2020-10-29 12:24:28
【问题描述】:
我使用angularjs 1.5版本,目的是使用组件来监听服务更新。
myService.ts
export class myService {
private _myBoolean: Subject<boolean>
constructor() {
this._myBoolean = new Subject<boolean>();
}
//
public get myBoolean(): Observable<boolean>{
return this._myBoolean;
}
public set myBoolean(value: Observable<boolean>) {
this._myBoolean.onNext(true)
}
}
我想监听来自服务的布尔更新 我的组件
export class myComponent ContextComponent {
public updatedBoolean;
$inject: string[] = [myService]
this.updatedBoolean = this.myService.myBoolean <--- I need to subscribe to the service
}
我尝试使用 angularJS 订阅来自服务的传入更新以更新我的组件变量。
【问题讨论】:
-
什么不起作用?
export class myComponent ContextComponent看起来像一个语法错误,你需要implements或extends在那里。在 angularjs 中使用Observable对象可能也不会像您期望的那样工作,Observable不会自动绑定到角度摘要循环中,您必须手动启动摘要循环。 -
是否可以举个例子使用上面的服务和组件和摘要循环?
标签: angularjs typescript rxjs