简体   繁体   English

AngularJS路由导致服务器调用问题

[英]AngularJS routing causing issues with server calls

I am using AngularJS in a nodewebkit application. 我在nodewebkit应用程序中使用AngularJS
I have three views: 我有三种看法:

  1. Home.html Home.html中
  2. Conversation.html Conversation.html
  3. Login.html 的login.html

On login, I am calling 登录时,我正在打电话

$state.go('home.main');    

which calls 哪个电话

$stateProvider.state('home.main', {
            url: '/home',  
            views: {
                "mainContent": {
                    templateUrl: 'views/home.html',
                    controller: 'loginController'
                }
            }
}

In main html, I am making a server call (through socket.io) to get all conversations. 在主html中,我正在进行服务器调用(通过socket.io)以获取所有对话。 Since data is huge, it takes some time to load it and in this time gap ie before user gets response from server, If user clicks on Logout, It takes user back to login page. 由于数据量巨大,因此需要花费一些时间来加载数据,并且在这个时间间隔内,即在用户从服务器获得响应之前,如果用户单击注销,它将使用户返回登录页面。

 $scope.logout = function(){  
    //Logout Logic  
      $state.go('login');
 }  

ie it calls 即它调用

 $stateProvider.state('login', {
            url: '/login',
            templateUrl: 'views/login.html',
            controller: "loginController"
        });  

Now When a user is trying to login, server responds to call made for getting conversations allowing user to take him to /home/conv.html. 现在,当用户尝试登录时,服务器会响应呼叫以进行对话,从而允许用户将其转到/home/conv.html。

I don't want to disable Logout button while data is being sent from server to client. 从服务器发送数据到客户端时,我不想禁用Logout按钮。
Is there any way on routing from /home to /login, we can cancel all server calls? 从/ home到/ login路由有什么办法,我们可以取消所有服务器调用吗?

I copied the code from https://github.com/angular-ui/ui-router/wiki : 我从https://github.com/angular-ui/ui-router/wiki复制了代码:

$stateProvider.state("contacts", {
  template: '<h1>{{title}}</h1>',
  resolve: { title: 'My Contacts' },
  controller: function($scope, title){
    $scope.title = 'My Contacts';
  },
  onEnter: function(title){
    if(title){ ... do something ... }
  },
  onExit: function(title){
    if(title){ ... do something ... }
  }
})

The onExit event will be triggered when you move from home.main to another route. 当您从home.main移到另一条路线时,将触发onExit事件。 So you should register to handle the event at where you declare the home.main route. 因此,您应该在声明home.main路由的地方进行注册以处理该事件。 In this event, you can cancel all server calls. 在这种情况下,您可以取消所有服务器调用。 As you are using socket.io, I suggest you use the function socket.removeAllListeners(); 在使用socket.io时,建议您使用函数socket.removeAllListeners();

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

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