[英]AngularJS Unknown Provider Error Message
I have an angular app that will not load and is returning the error message: 我有一个不会加载的角度应用程序,并返回错误消息:
Error: [$injector:unpr] Unknown provider: navigatorProvider <- navigator <- LayoutController
错误:[$ injector:unpr]未知提供程序:navigatorProvider <-导航器<-LayoutController
This is ever since I introduced a service I have this registered on the controller, but it still not working. 自从我引入一项服务以来,就已经在控制器上注册了该服务,但是它仍然无法正常工作。 The controller referenced is:
引用的控制器为:
(function () {
"use strict";
angular.module("HtJobPortal").controller("LayoutController",LayoutController);
LayoutController.$inject = ["$scope", "navigator"];
function LayoutController($scope, navigator){
var layout = this;
// Layout
layout.loadTemplate = function() {
return navigator.loadTemplate();
}
// Initialise pending and set roles
layout.init = function () {
// Global Start up
};
layout.init();
}
})();
This is the service: 这是服务:
(function() {
var navigator = angular.module('navigator', []);
navigator.factory('loadTemplate', function () {
var loadTemplate = this;
// Page Directory
navigator.login = "templates/login.html";
navigator.dashboard = "templates/dashboard.html";
navigator.job = "templates/job.html";
// Template switcher
navigator.loadTemplate = function () {
return navigator.login;
}
return loadTemplate;
});
}());
And the app page just in case I've missed something there: 和应用页面,以防万一我错过了一些东西:
(function () {
'use strict';
angular.module('HtJobPortal', []);
})();
You are add the dependencies when defining the HtJobPortal
module 您将在定义
HtJobPortal
模块时添加依赖HtJobPortal
//define dependencies
angular.module('HtJobPortal', ['navigator']);
In the controller, you need to inject the factory in controller 在控制器中,您需要在控制器中注入工厂
(function () {
"use strict";
angular.module("HtJobPortal").controller("LayoutController",LayoutController);
LayoutController.$inject = ["$scope", "loadTemplate"];
function LayoutController($scope, loadTemplate){
var layout = this;
// Layout
layout.loadTemplate = function() {
return loadTemplate.loadTemplate();
}
// Initialise pending and set roles
layout.init = function () {
// Global Start up
};
layout.init();
}
})();
And define factory as 并将工厂定义为
(function () {
angular.module('navigator', []).factory('loadTemplate', function () {
// Page Directory
var login = "templates/login.html";
var dashboard = "templates/dashboard.html";
var job = "templates/job.html";
return {
// Template switcher
loadTemplate: function () {
return login;
}
};
});
})();
To create a factory\\service\\controllers
, you generally don't require a new module every time. 要创建
factory\\service\\controllers
,通常不需要每次都需要一个新模块。 It's preferred to declare one module
and register your controller\\factory\\services
to same. 最好声明
one module
并将您的controller\\factory\\services
注册到相同的模块。
In your case, you can do it like: 就您而言,您可以按照以下方式进行操作:
(function() {
angular.module('HtJobPortal', [..define other module dependency here..]);
angular.module('HtJobPortal')
.factory('loadTemplate', function () {
var loadTemplate = {};
// Page Directory
loadTemplate.login = "templates/login.html";
loadTemplate.dashboard = "templates/dashboard.html";
loadTemplate.job = "templates/job.html";
// Template switcher
loadTemplate.loadTemplate = function () {
return loadTemplate .login;
}
return loadTemplate; // return object from factory
})
.controller("LayoutController",LayoutController);
LayoutController.$inject = ["$scope", "loadTemplate"]; //inject factory
function LayoutController($scope, loadTemplate){
var layout = this;
// Layout
layout.loadTemplate = function() {
return loadTemplate.loadTemplate(); // call factory method
}
// Initialise pending and set roles
layout.init = function () {
// Global Start up
};
layout.init();
};
}());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.