【发布时间】:2015-02-08 15:20:24
【问题描述】:
我正在尝试在 Service 内部创建一个角度函数,以通过 $http 返回访问数据,然后返回到所需的范围。
所以我的服务是这样的;
app.service('agrService', function ($http) {
this.testinho = function(){
return "teste";
}
this.bannerSlides = function(){
var dataUrl = "data/banner-rotator.json";
// Simple GET request example :
$http({
method: 'GET',
dataType: "json",
url: dataUrl
})
.success( function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
//console.log(data);
return data;
}).error( function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
alert("Niente, Nada, Caput");
});
}
})
然后我想将返回的数据与我的主 App 控制器内的范围相关联......就像这样:
app.controller('AppCtrl', function($scope, $http, agrService) {
$scope.slides = agrService.bannerSlides();
})
然后在我的模板中,我想像这样循环数据:
<div ng-repeat="slide in slides">
<div class="box" style="background: url('{{ slide.url }}') no-repeat center;"></div>
</div>
问题是数据只有在成功时才可用,我不知道如何将它传递给我的示波器幻灯片!!!!!!
我做错了什么?
在此先感谢
【问题讨论】: