gpt4 book ai didi

javascript - Angular : [$injector:unpr] Unknown provider:

转载 作者:行者123 更新时间:2023-12-03 07:49:03 25 4
gpt4 key购买 nike

routes.js

angular
.module('main')
.config(config);

config.$inject = ['$routeProvider', '$httpProvider'];

function config($routeProvider){

$routeProvider
.when('/', {
templateUrl:'main/views/landing.client.view.html',
controller:'MainController',
controllerAs:'mainCtrl',
resolve: {
orgTypes: orgTypes
}
})
.otherwise({
redirectTo:'/'
});

}


function orgTypes($http){

return $http
.get('emrsvs/orgTypes')
.then(function successCallBack(response){
return response;
}, function errorCallBack(error){
console.log(error);
});

}

controller.js

 angular
.module('main')
.controller('MainController', MainController);

MainController.$inject = ['$rootScope', '$timeout', 'orgTypes'];

function MainController($rootScope, $timeout, orgTypes){

var mainCtrl = this;
mainCtrl.orgTypes = orgTypes;

}

错误

[$injector:unpr] Unknown provider: orgTypesProvider <- orgTypes <- MainController

在这里,我从路由注入(inject)依赖项“orgTypes”到 Controller 。它产生了未知的提供者错误。我的系统有什么问题吗?有人能发现我的错误

最佳答案

您应该在routes.js中的orgTypes函数定义之前包含以下代码

angular.module('main')
.factory('orgTypes', orgTypes);
orgTypes.$inject = ['$http'];
/*You need to apply following changes also in controller*/
angular
.module('main')
.controller('MainController', MainController);

MainController.$inject = ['$rootScope', '$timeout', 'orgTypes'];

function MainController($rootScope, $timeout, orgTypes){

var mainCtrl = this;
orgTypes.then(function(response){
mainCtrl.orgTypes = response;
})

}

关于javascript - Angular : [$injector:unpr] Unknown provider:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35078289/

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