如何从callback method
AngularJs
注册/订阅controller
到AngularJs
service
?
我想在我的' 加载程序'时设置$scope.property
值在我的服务中运行已达到100%。
我想从我的Boolean
向我的true
返回controller
service
。
我想到了以下内容:
在我的method
中创建一个controller
,该service
已在我的service
中注册,当我的this._scope.loaderHasLoaded = this._progressBarService.loaderHasCompleted.push(function done(): boolean{})
中的加载程序达到100%时,该public loadingComplete():boolean{
if (this.isLoaderLoaded === true) {
return true
} else {
return false;
}
}
// some missing functionality
public loaderHasCompleted =[];
会被调用。我不确定自己是否走在正确的轨道上,以及如何填补缺失的部分。
控制器:
{{1}}
服务
{{1}}
非常感谢您的帮助!
答案 0 :(得分:0)
在我的服务中,我有以下代码:
在服务中:
// declare an array that will hold all my callback functions to be called after the loader has completed
private _callbacks = [];
// The method that will be called from the Controller to push the callback method into the _callbacks array
public listenLoadedComplete(callback: Function) {
this._callbacks.push(callback);
}
// In the actual loader method set the isLoaded variable to true and call the loadingComplete method
this.isLoaderLoaded = true;
this.loadingComplete();
// The loadingCompleted method executes each registered method in the _callback array
public loadingComplete(): boolean {
if (this.isLoaderLoaded === true) {
for (let cb of this._callbacks) {
cb();
}
return true
} else {
return false;
}
}
在控制器中:
// Call the listenForLoadCompletion method
this.listenForLoadCompletion();
// Register the callback function in the _callback array in the Service
// After the service has completed and called the method loadingComplete, the callback will be called
public listenForLoadCompletion(): void {
this._progressBarService.listenLoadedComplete(() => {
this._scope.loaderHasLoaded = true;
});
}