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

标签 javascript html angularjs

我有一个 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/

相关文章:

javascript - jQuery添加的反斜杠怎么去掉

javascript - 如何完全改变div onclick的内容?

javascript - 如何捕获 Action 窗口:beforeunload Angular 8

html - 将图像(在网格中)放置在 div 旁边?

javascript - Angular promise - POST 请求

javascript - JQuery函数中如何将事件作为参数传递

html - 为什么我的服务器会在 index.html 中添加神秘样式?

angularjs - Angular $resource 和自托管 WebAPI 的 CORS 问题

angularjs - 使用 http 请求与手动搜索数组

html - 我的 YouTube 视频不会显示在 iframe 中