我对AngularJs比较新,我有一个问题,当数据来自HTTP请求时使用自定义指令。AngularJs指令与HTTP请求数据
我有一个HTTP请求服务。
app.service('someService', function($http, $q){
this.getData = function(){
var deferred = $q.defer();
$http({
method: 'GET',
url: 'theUrl'
})
.success(function(data, status){
deferred.resolve(data);
})
.error(function(data, status){
deferred.reject;
})
return deferred.promise;
}
})
和一个调用服务的控制器。
app.controller('someConroller', function($scope, someService){
someService.getData().then(function(response){
$scope.data = response;
})
$scope.someArrayData = [
{.....}, {.....}, ...
]
}
这是一个非常简单的自定义指令。
app.directive('customDirective', function(){
return {
link: function(scope, element, attrs){
console.log(scope[attrs['customDirective']]);
}
}
})
的问题是,当我使用someArrayData
指令的情况下正常工作。但是当我使用data
(我从http服务获得的数据)获取指令的实例时,console.log(data)给了我未定义的内容。
<div custom-directive="someArrayData"></div><!-- console.log(scope[attrs['customDirective']]) gives the expected result -->
<div custom-directive="data"></div><!-- console.log(scope[attrs['customDirective']]) undefined -->
感谢您的帮助。
是否HTTP响应包含在你所期望的格式所需的数据? –
是的,它包含数据。 –