0

I need to inject the $route variable into the factory below in order to get the GET request to refresh. How would I inject it into the factory below?

.factory('posts', ['$http', function($http){
  //other factory functions

  o.factorySubmit = function() {
        $http.get('http://localhost:8080/clients').then(function(){
        $route.refresh();
      });
  };

  return o;
}])
3
  • 2
    Same as you done for $http. Note that you are maintaining sequence. As they get mapped. Commented Oct 4, 2016 at 20:28
  • As soon as I add the ['$http', '$route', function($http, $route){ the application stops working Commented Oct 4, 2016 at 20:34
  • make sure that you have added 'ngRoute' as dependency to your module. for example, angular.module('yourModule', ['ngRoute']) Commented Oct 5, 2016 at 11:17

1 Answer 1

2

You've already got Inline Array Notation set up for your Dependency Annotation for your factory method.

Here's how you would inject $route:

.factory('posts', ['$http', '$route', function($http,$route){
  //other factory functions

  o.factorySubmit = function() {
        $http.get('http://localhost:8080/clients').then(function(){
        $route.refresh();
      });
  };

  return o;
}])
Sign up to request clarification or add additional context in comments.

5 Comments

Hmm.. that's what I started with.. didn't work. Do I need to declare the $route elsewhere outside of the factory? I am using ui.router for routing..
This is the correct way of injecting routes. You don't need to declare $route anywhere else. If your application is generating an error, or stops working once you add this, then there is probably some other configuration issue in your application. Have you properly added ui.router as a dependency for your module(s)?
Yeah, I have and it works. But as soon as I add any second dependency to a factory, I get: Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! Watchers fired in the last 5 iterations: []
The infdig error that is thrown is the clue to the problem. Reference: infdig. I would bet that if you comment out the $route.refresh(); call that the error goes away, but it won't solve the issue. Since it sounds like you need to refresh the route upon successfully getting some information from the server, why not use an angular resolve for the route instead and get the information before going to that route? Reference: ui-router resolve
Also, this issue your bring up is a separate issue entirely, you may need to post a separate question to resolve this issue if you need further guidance on it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.