1

So I am doing an http request in my router thrugh resolve and I am wondering on how can I get a value from a factory inside my http request? I have a function in my factory that I want to call and get a value from and send that value through the http request. Is there any way I can inject a factory in the resolve function?

  $routeProvider
    .when('/', {
      redirectTo: '/home'
    })
    .when('/home', {
      templateUrl: 'views/main_portal.html',
      controller: MyController15,
      resolve: MyController15.loadAll
        })
    .when('/home/convo', {
      templateUrl: 'views/convo.html',
    })
    .otherwise({
      redirectTo: '/home'
    });


MyController15.loadAll = {
  //I WANT TO PASS A VARIABLE HERE TO USE IN THE PARAMS OBJECT.
  allQ: function($http) {
        return $http({
        url: some_url,
        method: post,
        params: {action: 'someAction', valueToPass: ???????}
      });
    }
};

var MyController15 = ['allQ',
  function(allQ){
//some data manipulation of allQ.data

}];

2 Answers 2

2

Maybe you should just resolve some parameters and then the controller can use $http or a service to get your data... Like so:

$routeProvider.when('/home/:valueToPass', {
                  templateUrl: 'views/main_portal.html',
                  controller: 'MyController15',
                  resolve: {valueToPass: function($route){
                      return $route.current.params.valueToPass;
                  }}
              });     

//...

app.controller('MyController15', function($scope, $http, valueToPass) {

//...

Or can you use this jsFiddle to illustrate your problem?

http://jsfiddle.net/malix/BEscs/

Sign up to request clarification or add additional context in comments.

Comments

0

You should restructure your http requests into a service. Probably the same service where you are getting the value from. Just make sure to return the http promise in the services methods.

2 Comments

The problem is that I need the data from my the http request and if I put it in the service, I get an undefined error because the http request does not load before the view loads. The way I have set it up right now has taken care of the problem of loading my view before my data loads and then the data simply refreshes itself. So again, the question is how can I use it in the same fashion but by using a factory/service method in the resolve.
If you had a plunkr or something this would be a little bit easier to help you, because what I described should work.

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.