Reputation: 2378
I am new to AngularJS. I am trying to make an $http api
call inside app config
of angularjs .Below is the code for $http api call.
.when('/programme/:programmename', {
templateUrl:"programme/info.html" ,
resolve: {
execute: function(userAPI,$rootScope,$route){
var url = $route.current.params.programmename;
url = url.replace(/-/g, ' ') ;
userAPI.getProgrammeid({programmename: url } , function(r){
$rootScope.Programmeid= r.getprogrammeidbyname.programmeidbyname.programmeid }
)}
}
})
As per Angularjs documentation resolve block executes before controller and template, however in my case controller is executing before resolve block
I tried out promises but I am getting stuck in it.
Is this because of $http api call which is (userAPI.getProgrammeid({programmename: url }.....)
inside resolve block
?.
I am a bit confused
please help me out this.
Upvotes: 0
Views: 612
Reputation: 3270
Does execute
actually return anything? It needs to return a promise that is resolved when the your function is done doing it's thing.
I think you are misunderstanding how to use resolve. It's to fetch something to inject into your controller. If the goal is to get the programmeid
, then the resolve would look something like this:
resolve: {
programmeid: function (userAPI, $q, $route) {
var deferred = $q.defer();
var url = $route.current.params.programmename;
url = url.replace(/-/g, ' ') ;
userAPI.getProgrammeid({ programmename: url }, function (r) {
deferred.resolve(r.getprogrammeidbyname.programmeidbyname.programmeid);
// now that our promise is resolved, our controller should execute
});
return deferred.promise;
}
}
and then you can get access to it in your controller like so:
app.controller('someCtrl', function ($scope, programmeid) {
$scope.programmeid = programmeid;
});
Upvotes: 2