angularjs和json服务器错误:[$ injector:unpr]未知提供者:$ resourceProvider< - $ resource< - menuFactory

时间:2017-03-10 13:00:24

标签: javascript angularjs json

大家好我开发了小的angularjs应用程序并使用json服务器来运行我的后端代码。我的代码中的提供程序存在问题。当我跑步时,我在下面遇到错误:

  

未捕获的TypeError:b.module(...)。info不是函数       at angular-resource.min.js:6       at angular-resource.min.js:14   angular.js:12808错误:[$ injector:unpr]未知提供者:$ resourceProvider< - $ resource< - menuFactory   http://errors.angularjs.org/1.4.14/ $注射器/ unpr?P0 =%24resourceProvider%20%3 C-%20%24resource%20%3 C-%20menuFactory       在http://127.0.0.1:3000/bower_components/angular/angular.js:68:12       在http://127.0.0.1:3000/bower_components/angular/angular.js:4381:19       at Object.getService [as get](http://127.0.0.1:3000/bower_components/angular/angular.js:4529:39)       在http://127.0.0.1:3000/bower_components/angular/angular.js:4386:45       at getService(http://127.0.0.1:3000/bower_components/angular/angular.js:4529:39)       在调用(http://127.0.0.1:3000/bower_components/angular/angular.js:4561:13)       在Object.instantiate(http://127.0.0.1:3000/bower_components/angular/angular.js:4578:27)       在对象。 (http://127.0.0.1:3000/bower_components/angular/angular.js:4438:24)       at Object.invoke(http://127.0.0.1:3000/bower_components/angular/angular.js:4570:17)       在Object.enforcedReturnValue [as $ get](http://127.0.0.1:3000/bower_components/angular/angular.js:4422:37

这是我的menufactory代码:

angular.module('confusionApp')
    .constant("baseURL","http://localhost:3000/")

        .service('menuFactory', ['$resource','baseURL', function($resource, baseURL) {

            var promotions = [
                {
                          _id:0,
                          name:'Weekend Grand Buffet', 
                          image: 'images/buffet.png',
                          label:'New',
                          price:'19.99',
                          description:'Featuring mouthwatering combinations with a choice of five different salads, six enticing appetizers, six main entrees and five choicest desserts. Free flowing bubbly and soft drinks. All for just $19.99 per person ',
                }

            ];

                this.getDishes = function () {
                  return $resource(baseURL+"dishes/:id",null, {'update': {'method':'PUT'}});
                };


                this.getPromotion = function (index) {

                    return promotions[index];

                };

        }])

这是控制器:

angular.module('confusionApp')

    .controller('MenuController', ['$scope', 'menuFactory', function($scope, menuFactory) {

        $scope.tab = 1;
        $scope.filtText = '';
        $scope.showDetails = false;
        $scope.showMenu = true;
        $scope.message = "Loading...";
        $scope.dishes = menuFactory.getDishes().query();
        $scope.select = function(setTab) {
            $scope.tab = setTab;

            if (setTab === 2) {
                $scope.filtText = "appetizer";
            }
            else if (setTab === 3) {
                $scope.filtText = "mains";
            }
            else if (setTab === 4) {
                $scope.filtText = "dessert";
            }
            else {
                $scope.filtText = "";
            }
        };

        $scope.isSelected = function (checkTab) {
            return ($scope.tab === checkTab);
        };

        $scope.toggleDetails = function() {
            $scope.showDetails = !$scope.showDetails;
        };
    }])

这是我的路线:

'use strict';

angular.module('confusionApp', ['ui.router', 'ngResource'])
.config(function($stateProvider, $urlRouterProvider) {
        $stateProvider

            // route for the home page
            .state('app', {
                url:'/',
                views: {
                    'header': {
                        templateUrl : 'views/header.html'
                    },
                    'content': {
                        templateUrl : 'views/home.html',
                        controller  : 'IndexController'
                    },
                    'footer': {
                        templateUrl : 'views/footer.html'
                    }
                }

            })

            // route for the aboutus page
            .state('app.aboutus', {
                url:'aboutus',
                views: {
                    'content@': {
                        templateUrl : 'views/aboutus.html',
                        controller  : 'AboutController'                  
                    }
                }
            })

            // route for the contactus page
            .state('app.contactus', {
                url:'contactus',
                views: {
                    'content@': {
                        templateUrl : 'views/contactus.html',
                        controller  : 'ContactController'                  
                    }
                }
            })

            // route for the menu page
            .state('app.menu', {
                url: 'menu',
                views: {
                    'content@': {
                        templateUrl : 'views/menu.html',
                        controller  : 'MenuController'
                    }
                }
            })

            // route for the dishdetail page
            .state('app.dishdetails', {
                url: 'menu/:id',
                views: {
                    'content@': {
                        templateUrl : 'views/dishdetail.html',
                        controller  : 'DishDetailController'
                   }
                }
            });

        $urlRouterProvider.otherwise('/');
    })
;

感谢您的阅读。

3 个答案:

答案 0 :(得分:0)

您是否在同一模块名称上第二次调用angular.module来定义控制器?e

您是否尝试过仅调用angular.module(' name')一次,然后在返回的实例上构建:

var myApp = angular.module(' confusionApp')     .constant(" baseURL"," http://localhost:3000/")....

然后:

myApp.controller(...)

如果不能解决问题,请分享你的root angular.module定义

答案 1 :(得分:0)

我解决了问题。问题不在我的代码中。我知道这是不合逻辑的但是当我使用angular.js时,来自凉亭的angular-ui-router.js和angular-resource.js我得到了上面的错误。将凉亭组件更改为CDN后,它开始运行良好。所以与凉亭有关的问题不是我的代码。注意:我在bower和CDN中使用了相同版本的angular.js,angular-ui-router.js和angular-resource.js

答案 2 :(得分:0)

我认为它与angular和ngResource版本之间的版本差距有关。对于ngResource(1.6)上的最新版本,你的角度版本(1.4)太旧了。

如果你想使用相同的角度版本,请下载角度资源版本1.4.8,或者在你的凉亭中,放置"angular-resource": "1.5.8"(不用^下载指定的版本号)

相关问题