为什么会出现依赖性错误?

时间:2015-07-11 19:31:19

标签: angularjs

我正在研究我的angularjs教程。

此处模块定义:

(function () {
    "use strict";
    var myApp = angular.module("sensorManagement",[]);
}());

这是资源定义: (function(){     “使用严格”;

angular.module("sensorManagement").factory("GetSensorDataResource",
                                        ["$resource",
                                         "ngResource",
                                         GetSensorDataResource])

    function GetSensorDataResource($resource) {
           return $resource("http://localhost:7486/api/Sensor/:id")
    }
}());

这是控制器定义:

(function () {
    "use strict";
    angular.module("sensorManagement").controller("SensorDataManagement",
                                                  ["GetSensorDataResource",
                                                  SensorDataManagement]);

    function SensorDataManagement(GetSensorDataResource) {
        var vm = this;

        GetSensorDataResource.query(function (data) {
            vm.sensorData = data;
        });
    }
})();

所有功能都在单独的文件中定义。按此顺序我调用文件:

,          

但是当我运行这个例子时,我得到了这个错误:

    Error: [$injector:unpr] Unknown provider: $resourceProvider <- $resource <- GetSensorDataResource
http://errors.angularjs.org/1.4.1/$injector/unpr?p0=%24resourceProvider%20%3C-%20%24resource%20%3C-%20GetSensorDataResource
    at REGEX_STRING_REGEXP (angular.js:68)
    at angular.js:4255
    at Object.getService [as get] (angular.js:4402)
    at angular.js:4260
    at getService (angular.js:4402)
    at Object.invoke (angular.js:4434)
    at Object.enforcedReturnValue [as $get] (angular.js:4296)
    at Object.invoke (angular.js:4443)
    at angular.js:4261
    at getService (angular.js:4402)

我知道为什么会收到错误?如何解决?

1 个答案:

答案 0 :(得分:1)

您需要加入angular-resource

ngResource docs

angular.module('sensorManagement', ['ngResource']);

如果不加载脚本并将ngResource模块注入您自己的模块,您将无法访问$resource$resourceProvider,因为它不包含在角度核心中。