Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在不加载angularjs中的当前页面的情况下重定向到其他页面_Angularjs_Node.js_Mean Stack - Fatal编程技术网

如何在不加载angularjs中的当前页面的情况下重定向到其他页面

如何在不加载angularjs中的当前页面的情况下重定向到其他页面,angularjs,node.js,mean-stack,Angularjs,Node.js,Mean Stack,我想重定向到另一个页面而不加载当前页面 让我解释一下我的任务。我有用户登录条件 如果用户还没有登录,他试图直接输入URL(localhost/sample.html),这意味着它将进入登录页面。对我来说,这种情况很好 首先打开sample.html页面,然后只有它会重定向到登录。用户能够在sample.html中看到数据 var logincheck = function () { debugger; $http.get('loggedin').success(function

我想重定向到另一个页面而不加载当前页面

让我解释一下我的任务。我有用户登录条件

如果用户还没有登录,他试图直接输入URL(localhost/sample.html),这意味着它将进入登录页面。对我来说,这种情况很好

首先打开sample.html页面,然后只有它会重定向到登录。用户能够在sample.html中看到数据

var logincheck = function () {
    debugger;
    $http.get('loggedin').success(function (user) {
        alert(user);
        // Authenticated
        if (user != '0') {
            refresh();
            return;

        }
            // Not Authenticated
        else {
            $window.location.href = '/';
        }
    });
};
logincheck();

是否有任何方法可以在不加载sample.html页面的情况下进入登录页面。

一个有趣的方法是在列表中检查此条件。例如,您可以在
应用程序中编写此代码。运行

$rootScope.$on('$routeChangeStart', function(event, next, current) {
    if(next.$$route && userNotAuthenticated) {
        $location.href('/');
    }
});
其优点是,它适用于您的整个应用程序,您不必为每个页面编写代码。

在主控制器(附加到索引的控制器)中,您应该进行登录检查,并将变量设置为true/false。在控制器中,检查此变量,如果用户未登录,则执行$state.go()

在连接主控制器的索引文件中使用ng-Clope指令,如:

<body ng-controller="mainController" ng-cloak>


你可以阅读ng斗篷

这是你可以做到的

无论何时启动angular应用程序,它都会运行
run
功能。因此,在该函数中附加一个位置更改侦听器,如下所示

App.run(['$rootScope', '$http', '$urlRouter', function($rootScope, $http, $urlRouter) {
  $rootScope.$on('$locationChangeSuccess', function(event) {
    event.preventDefault();// prevents the location change
    $http({// make a http call to check if user is logged in or not.
      url: '/path/to/check/auth',
      method: 'post',
      data: {}
    }).then(function(response){
      if(response.data.isUserLoggedIn){//If user is logged in.
        $urlRouter.sync();
      }else{//user is not logged in
        window.location.assign('/login');
      }
    }, function() {
      window.location.assign('/login');
    });
  });
  $urlRouter.listen();
}]);