gpt4 book ai didi

javascript - Angularjs Controller 不是一个函数,在创建工厂文件后未定义

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

首先,我有一个名为 angular-controller.js 的文件,其中有我的应用程序 Controller ,并在我的 html ng-controller 中调用它,它可以工作。现在我决定制作另一个文件,一个工厂文件,将一些功能与 Controller 文件分开,但现在出现错误:错误:参数“LoginController”不是函数,未定义
在assertArg(angular.js:1099)
在assertArgFn(angular.js:1109)
在 Angular.js:4978
在 Angular.js:4560
在 forEach (angular.js:137)
在nodeLinkFn(angular.js:4545)
在compositeLinkFn (angular.js:4191)
在 publicLinkFn (angular.js:4096)
在 Angular.js:1660
在 Object.$eval (angular.js:8218)

这是我的 angular-controller.js (带有我的 Controller ):

var app = angular.module("StaffLogin", []);  

app.controller("LoginController", function($scope, $http, restService) {
$scope.stafflogins = [];
$scope.staffLoginForm = {
email: "",
pass: ""
};
$scope.tokenStaffForm = {
idtokenstaff: -1,
tokenstaff: ""
};
$scope.staffForm = {
idstaff : -1,
staffType: {
idstaffType: -1,
type: ""
},
name: "",
surname: "",
birthDate: "",
phone: "",
gender: true,
working: true,
staffLogin: {
idstaffLogin: -1,
email: "",
pass: ""
}
};

$scope.submitCredentials= function() {
restService.login();
console.log($scope.staffLoginForm);
/* $http({
method : 'POST',
url : 'http://localhost:8080/FoodDrinkDispener/rest/tokenstaff',
data : angular.toJson($scope.staffLoginForm),
}).then(function successCallback(response) {
if (typeof response.data === 'object'){
_logsuccess(response)
return response.status;
}
else
_logerror(response);
},function (response) {
console.log($scope.tokenStaffForm);
_logerror(response);
}
);*/

}



function _SetToken(CurrentToken) {
sessionStorage.setItem("token", (CurrentToken === null) ? null : JSON.stringify(CurrentToken));
//console.log(CurrentToken);
}

function _logsuccess(response) {
console.log("Loggato correttamente");
console.log(response.status);
var CurrentToken = response.data;
_SetToken(CurrentToken);
}

function _logerror(response) {
console.log("Login fallito");
console.log(response.status);
_SetToken(null);
}






console.log(sessionStorage.getItem.toString);
});

这是我的工厂文件,名为rest-services.js:

//'use strict';

var app = angular.module("StaffLogin", []);
app.factory("restService", function($scope, $http) {
var REST_URL = "http://localhost:8080/FoodDrinkDispener/rest/";

function _GetToken() {
var token = sessionStorage.getItem("token");
if (token !== null)
return JSON.parse(token);
}

function _SetToken(CurrentToken) {
sessionStorage.setItem("token", (CurrentToken === null) ? null : JSON.stringify(CurrentToken));
}

return {
login: function(loginform){
return $http({
method : 'POST',
url : REST_URL+'tokenstaff',
data : angular.toJson(loginform),
}).then(function successCallback(response) {
if (typeof response.data === 'object'){
// _logsuccess(response)
console.log("login andato a buon fine");
return response.status;
}
else
console.log("login error");
},function (response) {
console.log($scope.tokenStaffForm);
console.log("login error");
}
);
}

}


});

这是我的 html 文件,我将 Controller 称为“LoginController”

<!DOCTYPE html>
<!-- saved from url=(0066)https://hackerstribe.com/guide/IT-bootstrap-3.1.1/examples/signin/ -->
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="images/puzzle.png" />





<title>Login</title>

<!-- Bootstrap core CSS -->
<link href="./styles/bootstrap.min.css" rel="stylesheet">

<!-- Custom styles for this template -->
<link href="./styles/signin.css" rel="stylesheet">

<!-- Just for debugging purposes. Don't actually copy this line! -->
<!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->

<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>

<body ng-app="StaffLogin" ng-controller="LoginController" ng-fa>

<div class="container">

<form class="form-signin" role="form" ng-controller="LoginController" ng-submit="submitCredentials()" novalidate>
<h2 class="form-signin-heading">Hai bisogno di autenticarti</h2>
<input type="email" class="form-control" placeholder="Email address" required="" autofocus="" id="email" ng-model="staffLoginForm.email">
<input type="password" class="form-control" placeholder="Password" required="" id="password" ng-model="staffLoginForm.pass">
<label class="checkbox">
<input type="checkbox" value="remember-me"> Ricordami
</label>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>

</div> <!-- /container -->


<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js"></script>
<script src="scripts/angular.js"></script>
<script type="application/javascript"></script>
<script src="scripts/login-controller.js"></script>
<script src="scripts/rest-services.js"></script>

</body></html>

希望有人能帮助我

最佳答案

对于工厂文件rest-services.js,更改:

var app = angular.module("StaffLogin", []); 


`var app = angular.module("StaffLogin");` 
app.factory("restService", function($http) {
// code
});
  • 您无法将 $scope 添加为 Factory/services 中的依赖项。
  • 关于javascript - Angularjs Controller 不是一个函数,在创建工厂文件后未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41141451/

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