angular.js:13294错误:[$ injector:unpr]未知提供者:cityResourceProvider< - cityResource< - cityListCtrl

时间:2016-03-28 21:00:33

标签: javascript angularjs

我第一次使用angularJS并与服务和工厂合作创建一个web api REST调用。我甚至在通话之前收到此错误:

angular.js:13294 Error: [$injector:unpr] Unknown provider: cityResourceProvider <- cityResource <- cityListCtrl

app.js

(function() {
    "use strict";

    angular.module("politicalHub",["common.services"]);

}());

common.services.js

(function() {
    "use strict";

    angular.module("common.services", ["ngResource", "ngRoute"])
        .constant("appSettings", {
            serverPath: "http://localhost:49828/"
        });
});

cityResource.js

(function() {
    "use strict";

    function cityResource($resource, appSettings) {
        return $resource(appSettings.serverPath + "api/City");
    }

    angular
        .module("common.services", [])
        .factory("cityResource",
        [
            "$resource",
            "appSettings",
            cityResource
        ]);
}());

cityListCtrl.js(Controller)

(function() {
    "use strict";
    function cityListCtrl(cityResource) {
        var vm = this;

        cityResource.query(function(data) {
            vm.cities = data;
        });
    }

    angular
        .module("politicalHub",[])
        .controller("cityListCtrl",
        ["cityResource", cityListCtrl]);


}());

HTML

 <form>
              <div ng-controller="cityListCtrl as vm" align="center">
                  <div class="col-lg-12" style="padding: 0">
                      <select ng-options="city.city_name for city in cities" ng-model="city.name" class="form-control">

                      </select>
                  </div>
              </div>
          </form>

提前致谢!

2 个答案:

答案 0 :(得分:2)

您错过了致电common.services.js,因为其IIFE功能应该自行调用

(function() {
    "use strict";

    angular.module("common.services", ["ngResource", "ngRoute"])
        .constant("appSettings", {
            serverPath: "http://localhost:49828/"
        });
})();

答案 1 :(得分:0)

这解决了我的问题:

(function() {
    "use strict";

   var app = angular.module("politicalHub",["common.services"]);

}());