Javascript 在angularJS中加载页面之前获取数据
我正在尝试使用$http在页面加载之前获取一个下拉列表。我尝试了几种组合,但它总是给出相同的错误: 错误:[$injector:unpr]未知提供程序:officeListProvider错误表示“officeListProvider”不存在或不可见,您需要将其作为依赖项添加 请尝试以下更改:Javascript 在angularJS中加载页面之前获取数据,javascript,angularjs,Javascript,Angularjs,我正在尝试使用$http在页面加载之前获取一个下拉列表。我尝试了几种组合,但它总是给出相同的错误: 错误:[$injector:unpr]未知提供程序:officeListProvider错误表示“officeListProvider”不存在或不可见,您需要将其作为依赖项添加 请尝试以下更改: var ctrl = angular.module('myApp.controllers', []); 到 此外,请使用相同的服务名称,即srvOfficeList或officeList,并检查您的服务
var ctrl = angular.module('myApp.controllers', []);
到
此外,请使用相同的服务名称,即srvOfficeList或officeList,并检查您的服务工厂,它不正确-示例:
希望它能解决这个问题
发布问题时,请尝试创建一个代码笔(或类似工具),以便在其中尝试/修复答案并与您共享。在控制器中,您应该只调用officeList。这是工作表。我也用webapi代替你的url
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
});
谢谢苏里亚,根据你的建议,我更新了代码。现在console.log(data)[见上文]按预期打印1850条记录,但我应该如何将其连接到$routeProvider.resolve()方法。我想在页面初始化之前加载下拉列表。是否在页面初始化之前加载下拉列表?什么意思?数据在客户端javascript中,您需要使用AngularJS控制器将其导入html-如何将数据从resolve加载到控制器,请检查:我希望在AngularJS中加载页面之前从数据库获取数据。请查看下面的URL。我使用了你提到的URL,它们使用了like-resolve:{message:…..,然后在controller app.controller(“newcontroller”,function(message)中注入相同的URL。请参见此处,controller和routerProvider函数中的名称“message”是相同的,但当我执行相同操作时[请参见我刚刚更新的代码]injector服务无法识别提供程序。这是我收到的错误:错误:[$injector:unpr]未知提供程序:officeListProvider乐意随时提供帮助。
var ctrl = angular.module('myApp.controllers', ['myApp.services']);
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
});