gpt4 book ai didi

javascript - 以 Angular 登录后将数据发送到不同的 Controller

转载 作者:行者123 更新时间:2023-11-28 13:10:24 27 4
gpt4 key购买 nike

我有一个 Login.html 页面:

<form class="form" ng-submit="submit()">
<label for="id">id:</label>
<input type="text" class="form-control" id="id" ng-model="id">
<label for="pwd">pass:</label>
<input type="password" class="form-control" id="pwd" ng-model="password">
<button type="submit" id="submitSection">Submit</button>
</form>

点击提交按钮后,我会转到 Controller 并从 api 服务器获取数据。

如果 ID 和密码正确,我想转到我的 index.html 并将登录 Controller 中收到的响应发送到索引 Controller

这是我的登录 Controller :

var app = angular.module("fastFly", []);
app.controller("Login", function ($scope, $http, $window) {
var scope = $scope;
$scope.submit = function () {
// alert($scope.password);
$http.get('http://localhost:8080/api/users/' + $scope.id + '/' + $scope.password)
.success(function (response) {
if (response == null)
{
swal("ERROR","wrong user name or password", "error");
}
else
{
console.log(response);
}
});
});

如何将响应发送到索引 Controller ?

谢谢!

最佳答案

您可以使用服务来存储您的登录数据并从任何 Controller 使用它

例如:

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

app.controller("Login", function($scope, $http, $window, loginService) {
var scope = $scope;
$scope.submit = function() {
// alert($scope.password);
$http.get('http://localhost:8080/api/users/' + $scope.id + '/' + $scope.password)
.success(function(response) {
if (response == null) {
swal("ERROR", "wrong user name or password", "error");
} else {
console.log(response);
loginService.setData(response)
}
});
});
});

app.controller("other", function($scope, loginService) {
$scope.loginData = loginService.getData();
});

app.service("loginService", function() {
var loginData = null;

this.getData = function() {
return loginData;
}

this.setData = function(data) {
loginData = data;
}
});

关于javascript - 以 Angular 登录后将数据发送到不同的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43016015/

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