嗨,我花了两天时间尝试根据异步 http 服务的响应来创建视图。但这对我不起作用。谁能帮我吗?
这里有基本思想的代码,但原来的有点不同:
var myApp = angular.module('app', []);
myApp.directive('dashboard',['service1',function(service1){
return {
templateUrl: 'sometemplate.html',
controller: function ($scope) {
$scope.data = {};
service1.get('keyXXX');
$scope.$watch(service1.data['keyXXX'],function(n,o){
//how to make this work???
//I always get null or it would not get executed at all
});
}
}
}])
.service('service1',['someAsyncservice',function(someAsyncservice){
var _s={
data:{},
get:function(key){
if(this.data[key]==undefined)
{
this.data[key]={status:"loading",data:{}};
}
someAsyncservice().then(function(result){
_s[key].data=result;
_s[key].status="success";
});
}
}
return _s;
}])