How do I access the $locationProvider to configure it?

前端 未结 1 1423
萌比男神i
萌比男神i 2021-02-14 06:16

What is the correct way to get the $locationProvider configuration parameters from a service / controller ? When doing a simple dependency injection with func

1条回答
  •  孤独总比滥情好
    2021-02-14 06:39

    I also got this error.

    You're allowed to inject a $location into a controller, but not a $locationProvider.

    Instead, the $locationProvider can be injected into a config method:

    var app = angular.module("myApp", []);
    
    app.config(function($locationProvider) {
      $locationProvider.html5Mode(true);
    });
    
    app.controller("myCtrl", function($location) {
      $location.path("/some/path");
    });
    

    And since I made this additional mistake: it's not just that you should add an app.config bit, but also remember to remove $locationProvider from the controller arguments, or you'll keep getting this error.

    If I understand things correctly, provider configuration happens during the configuration phase of the app lifecycle, as opposed to the run phase. Thus this separation. You can read a bit more about these phases here.

    I suspect that the reason for the error message is that when you inject $foo into a controller, it looks for a $fooProvider. Thus when we injected a $locationProvider, it looked for a $locationProviderProvider.

    0 讨论(0)
提交回复
热议问题