Service代码:
app.service('getTicketList', function($q, $http){
this.data = {};
this.getData = function(id){
var deferred = $q.defer();
var path='ticket.action?method:projectTickets';
if(id)
path+='&projectId='+id.toString();
$http.get(path).then(function(d){
//success
this.data = d;
deferred.resolve(d);
},function(){
//failure
deferred.reject(d);
});
}
});
controller代码:
app.controller('projectController', function($scope,getTicketList) {
$scope.tickets=getTicketList.getData($scope.projectId).data.tickets;
});
controller的那句代码有问题。我用getTicketList.data也获取不到数据,是{}。而且不知道怎么把参数传进去。。。。
1.service里面用return代替this
2.调用的时候使用promise模式
这种传递参数的方式是不对的,没这么用过,这里有个项目,找到对应的controller.service看看
https://github.com/LittleDouBi/community
你应该使用promise模式: