gpt4 book ai didi

javascript - 无法将工厂连接到 Controller

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

我正在尝试实现用户身份验证,但不知何故我遇到了麻烦,因为它不起作用。我没有收到任何具体错误,但当我按下登录按钮时,它没有执行任何操作。当我在 Chrome 开发工具上打开“网络”选项卡时,我没有看到任何 API 调用。

这是我的authServices.js

angular.module('authServices', [])
.factory('Auth', ['$http', function ($http) {
var authFactory = {};

authFactory.doLogin = function (loginData) {
return $http({
method: 'POST',
url: 'http://localhost:3000/api/users/authenticate',
data: loginData
}).then(function (data) {
console.log(data.data.token);
return data;
})
};
return authFactory;
}]);

login.js

angular.module('logInUserCtrl', ['authServices'])
.controller('logCtrl', ['$scope', '$location', '$http', '$timeout',
'Auth', function ($scope, $location, $http, $timeout, Auth) {

$scope.doLogin = function (loginData) {
$scope.loading = true;
$scope.errorMsg = false;

Auth.login(app.loginData)
.then(function (data) {
if (data.data.success) {
$scope.loading = false;
$scope.successMsg = data.data.message + 'Redirecting...';
$timeout(function () {
$location.path('/')
}, 2000);
} else {
$scope.loading = false;
$scope.errorMsg = data.data.message;
}
})
}
}
])

和我的login.html

 <form ng-submit="logCtrl.doLogin(loginData)">
<label>E-Mail:</label>
<input class="form-control" type="text" name="email" placeholder="please enter email"
ng-model="loginData.email">
<br>
<label>Username:</label>
<input class="form-control" type="text" name="username" placeholder="please enter username"
ng-model="loginData.username">
<br>
<label>Password:</label>
<input class="form-control" type="password" name="password" placeholder="please enter password"
ng-model="loginData.password">
<br>
<button class="btn btn-primary" type="submit" formmethod="post">Login</button>
</form>

提前致谢!

最佳答案

由于您没有使用 Controller 作为语法,请将按钮功能更改为,

<form ng-submit="doLogin(loginData)">

还要确保您的 Controller 在 HTML 中看起来像,

<div ng-controller="logCtrl">

关于javascript - 无法将工厂连接到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45128851/

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