AngularJS [$ injector:unpr]未知提供程序:dataProvider<-data<-PageCtrl [英] AngularJS [$injector:unpr] Unknown provider: dataProvider <- data <- PageCtrl

查看:63
本文介绍了AngularJS [$ injector:unpr]未知提供程序:dataProvider<-data<-PageCtrl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了其他答案,到目前为止,没有任何帮助.我在文件中使用以下代码收到此错误:

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'])
        .config(['$routeProvider', function($routeProvider) {
            $routeProvider.when('/page/:pageId', {
                templateUrl: 'page/view.html',
                controller: 'PageCtrl',
                resolve: {
                    data: function($q, $http, $routeParams) {
                        var deferred = $q.defer();
                        $http({method: 'GET', url: 'http://....' + $routeParams.pageId})
                            .then(function(data) {
                            deferred.resolve(data);


       });
                    return deferred.promise;
                }
            }
        })
    }])
    .controller('PageCtrl', function ($scope, $rootScope, data) {
//do stuff
}

在app.js中,我有这个:

And in the app.js I have this:

angular.module('myApp', [
    'ui.bootstrap',
    'ngRoute',
    'ngTouch',
    'ngResource',
    'myApp.page'
]).
config(['$routeProvider', function($routeProvider) {
  $routeProvider.otherwise({redirectTo: '/'});
}]).
config(['$provide', Decorate])

一切正常,我使用HTTP方法获取数据没有问题,直到我开始使用Q库并将数据获取移至config部分.有小费吗?其他答案似乎都不起作用.预先感谢!

Everything was working correctly and I fetched the data with the HTTP method with no problems, until I started using the Q library and moved the data fetching into the config section. Any tips? None of the other answers seem to work. Thanks in advance!

推荐答案

出现问题是由于您使用ng-controller指令实例化具有动态依赖项data的控制器PageCtrl的事实.由路由器创建.因此,当您通过路由器解析注入动态依赖项并让路由器实例化控制器时,您不需要也不应通过ng-controller实例化控制器,由于注入器缺少依赖项可用性,它只会失败.路由器将管理控制器的实例化,并为您的相应视图进行设置.

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.

因此,只需从视图中删除ng-controller ,同时还要确保路由所代表的部分足够完整,足以代表与控制器功能相关的视图.我也看到了一个好习惯,不要以ng-controller开始局部视图,并用route实例化,这将有助于使局部视图在不同的控制器上更可重用.同样,在创建单元测试时,您可以轻松模拟动态依赖关系,并通过$controller服务将其提供.

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.

这篇关于AngularJS [$ injector:unpr]未知提供程序:dataProvider<-data<-PageCtrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆