Service code:
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 code:
app.controller('projectController', function($scope,getTicketList) {
$scope.tickets=getTicketList.getData($scope.projectId).data.tickets;
});
There is a problem with the controller code. I can't get the data using getTicketList.data, it's {}. And I don't know how to pass the parameters in. . . .
1. Use return instead of this in service
2. Use promise mode when calling
This way of passing parameters is wrong. I have never used it like this. There is a project here. Find the corresponding controller.service and have a look
https://github.com/LittleDouBi/community
You should use promise mode: