【问题标题】:Receiving an $injector:unpr error when using ngResource使用 ngResource 时收到 $injector:unpr 错误
【发布时间】:2017-08-30 03:47:56
【问题描述】:

我在我的项目中使用 ngResource 和 $resource 而不是 $http。当我切换到 $resource 时,我在运行程序时收到了 $injector:unpr 错误。我检查以确保所有内容都拼写正确。此外,我确保在 HTML 中调用它。

这是模块:

angular.module('confusionApp', ['ui.router', 'ngResource'])

这是我尝试使用的服务:

angular.module('confusionApp')
  .constant("baseURL","http://localhost:3000/")
  .service('menuService', ['$resource', 'baseURL', function($resource, baseURL) {
     this.getDishes = function() {
        return $resource(baseURL+"dishes/:id", null, {
            'update':{
                method:'PUT'
            }
        });

    };

这里是我调用函数的地方:

angular.module('confusionApp')
  .controller('MenuController', ['$scope', 'menuService', function($scope, menuService) {
        $scope.showMenu = true;
        $scope.message = "Loading ...";
        $scope.dishes = menuService.getDishes().query();

这是 HTML 脚本:

<script src="../bower_components/angular/angular.min.js"></script>
<script src="../bower_components/angular-ui-router/release/angular-ui-router.min.js"></script>
<script src="../bower_components/angular-resource/angular-resource.min.js"></script>

非常感谢任何帮助。

【问题讨论】:

标签: javascript angularjs dependency-injection angular-resource ngresource


【解决方案1】:

我发现问题实际上不在于任何代码本身。与我安装的 angular-resource 版本相比,我使用的 angular 版本已经过时。我通过删除 bower.json 中的依赖项来解决此问题,然后从 bower_components 中删除了该文件夹。然后我用我想要的版本规格重新下载了 angular-resource。

$ bower install angular-resource#1.4.8

【讨论】:

    猜你喜欢
    • 2020-02-23
    • 1970-01-01
    • 2019-10-20
    • 2014-08-01
    • 2016-06-14
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多