Javascript AngularJS未知提供程序错误消息
我有一个angular应用程序无法加载,正在返回错误消息:Javascript AngularJS未知提供程序错误消息,javascript,angularjs,Javascript,Angularjs,我有一个angular应用程序无法加载,正在返回错误消息: 错误:[$injector:unpr]未知提供程序:navigatorProvider在定义HtJobPortal模块时,您正在添加依赖项 //define dependencies angular.module('HtJobPortal', ['navigator']); 在控制器中,需要在控制器中注入工厂 (function () { "use strict"; angular.module("HtJobPorta
错误:[$injector:unpr]未知提供程序:navigatorProvider在定义
HtJobPortal
模块时,您正在添加依赖项
//define dependencies
angular.module('HtJobPortal', ['navigator']);
在控制器中,需要在控制器中注入工厂
(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();
}
})();
并将工厂定义为
(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;
}
};
});
})();
定义
HtJobPortal
模块时,需要添加依赖项
//define dependencies
angular.module('HtJobPortal', ['navigator']);
在控制器中,需要在控制器中注入工厂
(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();
}
})();
并将工厂定义为
(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;
}
};
});
})();
要创建
工厂\服务\控制器
,通常不需要每次都使用新模块。最好声明一个模块
,并将控制器\工厂\服务
注册到该模块
在您的情况下,您可以这样做:
(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();
};
}());
要创建
工厂\服务\控制器
,通常不需要每次都使用新模块。最好声明一个模块
,并将控制器\工厂\服务
注册到该模块
在您的情况下,您可以这样做:
(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();
};
}());
它在控制器中的调用方式是否仍然与navigator.loadTemplate相同?@WebDevelopWolf,您没有从工厂返回任何方法,我已经修改了它。它在控制器中的调用方式是否仍然与navigator.loadTemplate相同?@WebDevelopWolf,您没有从工厂返回任何方法,我已经修改了它