我的服务查询数据库。
periodDataSvc.periodData = {};
periodDataSvc.refresh = function(user) {
APIService.query({ route:'period', id: user._id }, function(data) {
periodDataSvc.periodData.orders = data[0].orders;
periodDataSvc.periodData.jobs = data[0].jobs;
periodDataSvc.periodData.board = data[0].board;
periodDataSvc.periodData.period = data[0].period;
periodDataSvc.periodData.warehouse = data[0].warehouse;
periodDataSvc.periodData.offers = data[0].offers;
})
}
在我的控制器中,我调用数据:
$scope.periodData = PeriodDataService.periodData;
但是当我加载带有指令的模板时,没有从控制器接收数据,并且该指令不起作用(抛出错误,例如来自过滤器)。
div(production-board="" period="periodData.period" data="periodData.board"
)
我如何保证在呈现指令之前加载数据?
谢谢!
答案 0 :(得分:0)
您应该使用$q服务。具有promise的Init变量periodData
,在查询结果和控制器使用中解析延迟:
PeriodDataService.periodData.then(function (data) { $scope.periodData = data; });
编辑2:
var deferred = $q.defer()
periodDataSvc.periodData = deferred.promise;
periodDataSvc.refresh = function(user) {
APIService.query({ route:'period', id: user._id }, function(data) {
deferred.resolve(data[0]);
})
}