繁体   English   中英

处理成功的node / angularjs登录而无需重新加载页面

[英]Handling successful node/angularjs login without reloading page

我正在尝试构建一个单页面节点/ angular(v 1.56)应用程序,该应用程序利用angular的ui-router更改应用程序内的页面,而无需重新加载任何浏览器。 我的主要障碍是,我试图弄清楚登录事件成功之后如何在无需重定向/重新加载该页面的情况下使用户进入仪表板页面? 理想情况下,我正在寻找一种方法来以编程方式触发路线,就像单击链接一样。

我尝试在loginAction发布响应后使用angular的$ http.get('/ dashboard')到目标路线,但这不起作用,因为$ http.get()与实际点击产生的GET调用完全不同在href =“ / dashboard”定位标记上。 后一种click事件将按其应有的方式调用仪表板页面,并将其呈现在索引页面上的标记中。 有没有一种“优美的”,有角度的方式来解决这个问题? 使用节点的快速Web服务器或利用文件流的自定义Web服务器时,此问题相同。

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>

<script>

 var app = angular.module('myApp',['ui.router']);

 app.config(function($stateProvider) {                                                            

     var aboutState =     {
                            name: 'about',  //for testing 
                            templateUrl: '/about'
                          };

     var dashboardState = {
                            name: 'dashboard',
                            templateUrl: '/dashboard'
                          };

     $stateProvider.state(aboutState);
     $stateProvider.state(dashboardState);

  });

调节器

  app.controller("myCtrl", function($scope, $http) {

      $scope.userMessage = "";

      $scope.loginSubmit = function () {

          $scope.userMessage = "Submitting...";

           $http.post("/loginAction",{ 'username': $scope.username, 'password':$scope.password }).then(function(response) {

          if( response.data.loginStatus == 'Authenticated' && response.data.userType != '' ) {

                // OK ! - we're free to go to the dashboard page now. But how ?  

               // I could do: document.querySelector("#dash").click(); 
               // this works, but this doesn't seem very secure

              // The following doesn't work: 

            $http.get("/dashboard").then(function( response ) {

                     // Why doesn't the above '/dashboard' route , but
                     // clicking on something like <a href="/dashboard">Dashboard</a> actually works ? 

                     // Short of taking the dashboard html in the response and using bind-html to force it
                     // into the dom, is there a better solution to avoid a window.location reload here ? 

                     $scope.userMessage = "Login Successful";
                });                    
              }
          });
        }
  });

我想我回答了我自己的问题。 我需要利用'ngRoute'服务并将$ location注入控制器,如下所示:

<script>

var app = angular.module('myApp',['ngRoute']);

app.config(function($ routeProvider){

              $routeProvider

              .when('/', {
                templateUrl : 'login',
                controller  : 'myCtrl'
              })
              .when('/test', {
                templateUrl : '/test',
                controller  : 'myCtrl'
              })
              .when('/dashboard', {
                templateUrl :'/dashboard',
                controller  : 'myCtrl'

              }).otherwise({redirectTo: '/'});

            });

app.controller(“ myCtrl”,function($ scope,$ http,$ location){

              $scope.userMessage = "";

               // fire this function upon successful login: 

               $scope.changeRoute = function( route ) { 

                      $location.path( route );
                }


              $scope.loginSubmit = function () {

                   $scope.userMessage = "Submitting...";

                   $http.post("/loginAction",{ 'username': $scope.username, 'password':$scope.password }).then(function(response) {

                  if( response.data.loginStatus == 'Authenticated' && response.data.userType != '' ) {

                     $scope.userMessage = "Authenticated...";
                     $scope.changeRoute( response.data.destination_route );

                    }
                  });
                }
          });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM