AngularJS [$injector:unpr] Unknown provider: dataProvider <- data <- PageCtrl

前端 未结 1 1712
我寻月下人不归
我寻月下人不归 2021-01-21 02:12

I have seen the other answers and so far nothing has helped me. I get this error with the following code in a file:

   angular.module(\'myApp.page\', [\'ngRoute\         


        
相关标签:
1条回答
  • 2021-01-21 02:34

    You issue is due to the fact that you are using ng-controller directive to instantiate the controller PageCtrl which takes a dynamic dependency data which is only created by the router. So when you inject a dynamic dependency via router resolve and having the router instantiate the controller, you do not need to and should not instantiate the controller via ng-controller it will simply fail due to the lack of dependency availability from the injector. router will manage the instantiation of the controller and setting it up for the respective view for you.

    So just remove ng-controller from your view also make sure the partial represented by the route is complete enough to represent the view related to the controller functionality. Also i have seen it is a good practice not to start with a partial view with ng-controller and instantiate with route which will help making that partial view more reusable with a different controller. Also while creating a unit test you can easily mock the dynamic dependency and feed it via the $controller service.

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