0

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.

2
  • Why did you put Ruby tags on this question? Commented Nov 29, 2013 at 7:35
  • @CWSpear I am using Ruby on Rails as back end and AngularJs as front end.Sorry for the misunderstanding if any Commented Nov 29, 2013 at 7:40

1 Answer 1

2

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;
});
Sign up to request clarification or add additional context in comments.

3 Comments

@CWSpear.Thanks The code helped me and it is working perfectly.Thanks a lot. The angularjs documentation is not helping and takes time to understand.Can u recommend any other sites which i can visit to sharpen my skills.Am looking at egghead.io
@PeeVee Unfortunately, I don't have any other solid, reliable sources that I use. Just lots of Googling, and the docs are getting better. There was like 1 line of docs for resolve when I first tried figuring this out. Good luck!
thanks for the help...But the docs are not very good for beginners like myself but looking at various sources to get experience at angularjs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.