我正在尝试使用angular $ http在页面加载之前获取下拉列表。我尝试了几种组合,但它继续给出同样的错误:
错误:[$ injector:unpr]未知提供者:officeListProvider< - officeList< - myController http://errors.angularjs.org/1.4.3/ $ injector / unpr?p0 = officeListProvider%20%3C-%20officeList%20%3C-%20myController
我有几个星期的角度所以请原谅任何愚蠢的错误。
var myApp = angular.module('myApp',['ngRoute']);
myApp.config(['$routeProvider',function ($routeProvider) {
$routeProvider.when('../../home/goEeUpdateAngular.obj', {
templateUrl: '/employee_update_angular.jsp',
controller: 'myController',
resolve: {
officeList: function(officeListFactory) {
return officeListFactory.getOfficeList();
}
}
});
}]);
myApp.factory('officeListFactory', function($http, $window) {
$window.alert("Hi");
var factoryResult = {
getOfficeList: function() {
var promise = $http({
method: 'GET',
url: '../../home/goOfficesList.obj'
}).success(function(data, status, headers, config) {
console.log (data);
return data;
});
return promise;
}
};
console.log (factoryResult.getOfficeList());
return factoryResult;
});
myApp.controller('myController',function ($scope,officeList) {
$scope.officeListFactory = officeListFactory.data;
});
答案 0 :(得分:1)
错误说" officeListProvider"不存在或不可见,您需要将其添加为依赖项。
请尝试以下更改:
var ctrl = angular.module('myApp.controllers', []);
到
var ctrl = angular.module('myApp.controllers', ['myApp.services']);
并且请使用相同的服务名称srvOfficeList或officeList,并检查您的服务工厂,这是不对的 - 例如:AngularJS : factory $http service
希望它能解决这个问题。
请在发布问题时尝试创建CodePen(或类似工具),以便答案可以在那里尝试/修复并与您共享。
答案 1 :(得分:0)
在控制器中,您应该只调用officeList。这是工作JSFIDDLE。我也用webapi代替你的网址
var myApp = angular.module('myApp',['ngRoute']);
myApp.config(['$routeProvider',function ($routeProvider) {
$routeProvider.when('../../home/goEeUpdateAngular.obj', {
templateUrl: '/employee_update_angular.jsp',
controller: 'myController',
resolve: {
officeList: function(officeListFactory) {
return officeListFactory.getOfficeList();
}
}
});
}]);
myApp.factory('officeListFactory', function($http, $window) {
$window.alert("Hi");
var factoryResult = {
getOfficeList: function() {
var promise = $http({
method: 'GET',
url: '../../home/goOfficesList.obj'
}).success(function(data, status, headers, config) {
console.log (data);
return data;
});
return promise;
}
};
console.log (factoryResult.getOfficeList());
return factoryResult;
});
myApp.controller('myController',function ($scope,officeList) {
$scope.officeListFactory = officeList.data; //changes are made here
});