I am new to AngularJs and I created a service and want to call that service when a particular controller loads.
The code I used for controller is:
sampleApp.controller('ProjectAppController', function ($scope, ProjectAppService) {
ProjectBudgetService.save().then (
function () {
$location.url('');
},
function () {
$scope.error = true;
}
);
}
And the code for service is here:
sampleApp.factory('ProjectBudgetService', function ($http, $q) {
return {
save: function () {
var deferred = $q.defer();
$http.get('http://lh-cip.ultraserve.net.au/project/budget/?',
{ headers: { 'Authorization': $.session.get("loginToken") } }
)
.success(function (data) {
if(data.options=="") {
alert("No Budget Option.");
}
else {
$q.options.push.apply($q.options, data.options);
$q.services.forEach(function(entry){
alert(entry);
});
deferred.resolve();
}
})
.error(function (err) {
$('#al_msg').html('<div class="alert alert-danger"><span>Error! Can\'t Complete Login Process.</span><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button></div>'); deferred.reject();
});
return deferred.promise;
}
};
});
The problem is that this code is not working. I don't know where is the problem. Please help me to sort it out. Thanks