[英]What do I return for an Angular binded property while waiting for $onInit callbacks to complete
I have an angular service like: 我有一个角度服务,如:
appModule.factory('dataService', DataService);
DataService.$inject = ['dataContext'];
function DataService(dataContext) {
var service = Object.create(DataService.prototype);
service._dataContext = dataContext;
service._details = {};
service.$onInit();
return service;
}
DataService.prototype = {
$onInit: function () {
this._dataContext.combinedQuery(DataQuery)
.then(function (data) {
this._details = data[0];
}.bind(this));
},
Details: function () {
return this._details;
}
};
The Controller consuming it is like: 使用它的控制器如下:
DetailsController.$inject = ["dataService"];
function DetailsController(dataService) {
this._dataService = dataService;
}
DetailsController.prototype = {
$onInit: function () {
this.details = this._dataService.Details;
},
}
And the View is binding {{$DetailsController.details}}
. View正在绑定
{{$DetailsController.details}}
。
When running DataService.$onInit
will return, the DataService.Details
function return value will be bound to DetailsController.details
before this._details = data[0]
has been called. 当运行
DataService.$onInit
将返回时, DataService.Details
函数返回值将被绑定到DetailsController.details
之前this._details = data[0]
已被调用。
What should I be returning from DataService.Details
so that once this._details
is set, the data is shown on the bound view? 我应该从
DataService.Details
返回什么,以便一旦设置this._details
,数据就会显示在绑定视图上?
I think it should be something like below, but I don't want to call $onInit
again: 我认为它应该像下面这样,但我不想再次调用
$onInit
:
return this.$onInit()
.then(function () {
return this._details;
}.bind(this));
I would try using Promises
我会尝试使用
Promises
appModule.factory('dataService', DataService);
DataService.$inject = ['dataContext'];
function DataService(dataContext) {
DataService.prototype().then( function (err, prototype){
if(err){
return err;
}
//Else
var service = Object.create(prototype);
service._dataContext = dataContext;
service._details = {};
service.$onInit();
if(service){
return service;
}
});
}
DataService.prototype = {
return new Promise( function( resolve, reject){
$onInit: function () {
this._dataContext.combinedQuery(DataQuery)
.then(function (data) {
this._details = data[0];
}.bind(this));
}
if( this.details ){
Details: function () {
return resolve( this._details );
}
} else {
return reject({err: 'Unable to set details'})
}
});
};
I can't guarantee if the code above works 100% since I did not build a Plunker
or similar but, it really seems like it's an asynchronous
issue. 我不能保证上面的代码是否100%工作,因为我没有构建一个
Plunker
或类似的东西,但它看起来真的是一个asynchronous
问题。 If this does not work the, you should wrap $on.init()
into a Promise
also. 如果这不起作用,你应该将
$on.init()
包装到Promise
。
Anyways, I never seen writing services/factories
like you do, so I'm a bit confused about it. 无论如何,我从来没有见过像你这样写
services/factories
,所以我对它有点困惑。
Hope it helps a bit anyways. 希望它有点帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.