简体   繁体   English

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

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

I'm trying to build a single page node/angular ( v 1.56 ) application that leverages angular's ui-router to change pages inside the application without having any browser reloads. 我正在尝试构建一个单页面节点/ angular(v 1.56)应用程序,该应用程序利用angular的ui-router更改应用程序内的页面,而无需重新加载任何浏览器。 My main obstacle is that I'm trying to figure out how, after a successful login event, do I get the user to the dashboard page without having to redirect/reload that page? 我的主要障碍是,我试图弄清楚登录事件成功之后如何在无需重定向/重新加载该页面的情况下使用户进入仪表板页面? Ideally, I'm looking for a way to programmatically trigger a route just as if I had clicked on the link. 理想情况下,我正在寻找一种方法来以编程方式触发路线,就像单击链接一样。

I tried using angular's $http.get('/dashboard') to the target route after the loginAction post response, but this doesn't work, as $http.get() is quite different than a GET call that results from actually clicking on an href="/dashboard" anchor tag. 我尝试在loginAction发布响应后使用angular的$ http.get('/ dashboard')到目标路线,但这不起作用,因为$ http.get()与实际点击产生的GET调用完全不同在href =“ / dashboard”定位标记上。 The latter click event calls the dashboard page as it should, rendering it in the tag on the index page. 后一种click事件将按其应有的方式调用仪表板页面,并将其呈现在索引页面上的标记中。 Is there a 'graceful', angular way to handle this ? 有没有一种“优美的”,有角度的方式来解决这个问题? This issue is the same using node's express webserver or a custom webserver that leverages filestreams. 使用节点的快速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);

  });

controller 调节器

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

I think I answered my own question. 我想我回答了我自己的问题。 I needed to leverage the 'ngRoute' service and inject $location into my controller like so: 我需要利用'ngRoute'服务并将$ location注入控制器,如下所示:

<script>

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

app.config(function($routeProvider) { 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 ) { 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.

相关问题 Rails中的Angularjs:无需重新加载即可更改页面吗? - Angularjs in rails: Change page without reloading? 仅在angularjs中成功登录并隐藏菜单后才重定向到页面 - Redirect to page only after login is successful in angularjs and hide menu 如何在不重新加载页面的情况下将浏览器地址栏中的 URL 更改为 angularJS - How to change URL in browser address bar with angularJS without reloading the page Ajax 在登录表单中显示错误而不重新加载页面 - Ajax displaying errors in login form without reloading page 网站如何在不重新加载页面的情况下检查登录凭据? - How do websites check the login credentials without reloading the page? 发布请求而无需重新加载页面,并且不支持JavaScript(Node Express框架) - Post request without reloading page and support for no javascript (Node Express framework) 将确认警报从Node / Express发送到浏览器,而无需重新加载页面 - Sending confirmation alerts from Node/Express to Browser without reloading page 如何在不使用Angular和Node JS重新加载页面的情况下更改数据 - How to change data without reloading page with Angular and Node JS 没有angular-route的angularjs登录页面 - Login Page with angularjs without angular-route AngularJS / Firebase-需要登录两次才能成功登录 - AngularJS / Firebase - Need to login twice for a successful login
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM