gpt4 book ai didi

javascript - 在angularJS中加载页面之前获取数据

转载 作者:行者123 更新时间:2023-11-29 15:33:49 25 4
gpt4 key购买 nike

我正在尝试使用 Angular $http 在页面加载之前获取下拉列表。我尝试了几种组合,但它一直出现相同的错误:

错误:[$injector:unpr] 未知提供者:officeListProvider <- officeList <- myController http://errors.angularjs.org/1.4.3/ $injector/unpr?p0=officeListProvider%20%3C-%20officeList%20%3C-%20myController

我在 angular 方面才几周大,所以如果有任何愚蠢的错误,请原谅。

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;
});

最佳答案

错误提示“officeListProvider”不存在或不可见,您需要将其添加为依赖项。

请尝试以下更改:

var ctrl = angular.module('myApp.controllers', []);

var ctrl = angular.module('myApp.controllers', ['myApp.services']);

同时请使用相同的服务名称,即 srvOfficeList 或 officeList,并检查您的服务工厂,这是不正确的 - 例如:AngularJS : factory $http service

希望它能解决问题。

请在发布问题时尝试创建一个 CodePen(或类似工具),以便可以在其中尝试/修复答案并与您分享。

关于javascript - 在angularJS中加载页面之前获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31705267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com