gpt4 book ai didi

javascript - AngularJS $注入(inject)器: unknown provider

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

我有一个问题,我有这段代码:

index.html:

<!doctype html>
<html lang="nl" ng-app="tradePlace">
<head>
<meta charset="utf-8">
<link href="./helpers/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="./helpers/bootstrap/js/bootstrap.min.js" rel="stylesheet" type="text/css">
<link href="./helpers/style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.18/angular.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.18/angular-route.min.js"></script>
<script type="text/javascript" src="./includes/routes/routes.js"></script>
<script type="text/javascript" src="./includes/services/loginService.js"></script>
<script type="text/javascript" src="./includes/controllers/indexCntrl.js"></script>
</head>
<body ng-view></body>
</html>

路由.js:

var app = angular.module('tradePlace', ['ngRoute', 'tradeCntrls']);
app.config(['$routeProvider', '$locationProvider', function($route, $location) {
$route.
when('/', {
redirectTo: '/index'
}).
when('/index', {
templateUrl: './includes/templates/index.html',
controller: 'indexCntrl'
}).
when('/register', {
templateUrl: './includes/templates/register.php',
controller: 'indexCntrl'
}).
otherwise({
redirectTo: '/index'
});

$location.html5Mode(true);
}])

indexCtrnl.js:

var app = angular.module('tradeCntrls', []);

app.controller('indexCntrl', ['$scope' , 'loginService', function($scope, login){
$scope.login = function() {

}
}])

登录服务.js

app.factory('loginService', ['', function(){
return function login(){

};
}])

所以,我收到此错误:

Error: $injector:unpr Unknown Provider

AngularJS 文档上的错误页面显示:

This error results from the $injector being unable to resolve a required dependency. To fix this, make sure the dependency is defined and spelled correctly

嗯,我确定拼写正确并且应该对其进行定义。我已经尝试在 loginServices.js 中放置一个 getter 来获取 tradePlace 模块,并且还尝试将 getter 与 tradePlaceCntrls 模块一起使用,但我仍然收到错误。

我不知道如何解决这个问题。

最佳答案

不确定这是否是这样,但您是否尝试过像这样定义您的工厂?

app.factory('loginService', function(){
return function login(){

};
});

您不需要该数组,因为您的登录服务没有任何依赖项。 '' 可能会导致您出现问题。尝试删除该数组,因为在这种情况下没有必要。

此外,您可以将登录服务定义为服务:

app.service('loginService', function(){
return function login(){

};
});

关于javascript - AngularJS $注入(inject)器: unknown provider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24673676/

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