以角度登录后,将数据发送到其他控制器

时间:2017-03-25 11:54:30

标签: 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>
单击提交按钮后,我转到控制器并从api服务器获取数据。

如果id和密码正常,我想转到我的index.html并将我在Login控制器中获得的响应发送到索引控制器

这是我的登录控制器:

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);                       
               }
           });
  });

如何将响应发送到索引控制器?

谢谢!

3 个答案:

答案 0 :(得分:3)

您可以使用服务存储您的登录数据并从任何控制器

使用它

例如:

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;
    }
});

答案 1 :(得分:2)

您可以将 $ rootScope 用于商店登录数据并用于其他控制器

或者 - 您可以使用$ localstorage存储登录数据并使用其他控制器或任何地方


    $localStorage.LocalMessage = "value";

或 -


    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;
        }
    });

答案 2 :(得分:0)

我建议你使用satellizer

相关问题