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

时间:2018-11-18 02:22:31

标签: javascript angularjs node.js angular-ui-router

我正在尝试构建一个单页面节点/ 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";
                });                    
              }
          });
        }
  });

1 个答案:

答案 0 :(得分:0)

我想我回答了我自己的问题。我需要利用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 );

                    }
                  });
                }
          });