广播注销

时间:2017-03-19 09:06:43

标签: javascript node.js

有人在一个关于我无法再找到的资源的帖子上发布了与以下代码非常相似的内容:

$scope.logout = function(){ 
    var outRes = $resource("/admin/logout"); 
    outRes.save({}, 
    function () { 
        $scope.userObject.lastActivity_type = "Log out"; 
        $rootScope.isLoggedIn = false; 
        $location.path("/login-register"); 
    }, 
    function errorHandling(err) {
        console.log("Not cool, error"); 
    }); 
}

这对我的任务非常有帮助并且看起来相当近,所以我希望问:有人可以解释如何用广播方法重写这个,让其他用户/架构的每个部分都知道用户已经注销了吗?任务结束了一切,我只是好奇

1 个答案:

答案 0 :(得分:0)

Assuming this is for Angular 1, broadcasting an event makes it visible to all child scopes of the scope in which it is broadcast. If you want it to be visible to all scopes, you should broadcast it in the root scope, as in this example:

$rootScope.broadcast('my-event');

Documentation is here传递动态值。

编辑:我注意到您还询问了向其他用户广播的问题。那是一条非常不同的水壶!

Angular纯粹是一个前端框架,因此它无法向另一个客户端广播消息。如果您想要一些聊天或通知系统,用户可以实时收到其他用户所做更改的通知,您可能希望通过Socket.io等库来使用websockets。您可以通过websockets或AJAX请求向服务器发送某种通知,然后在服务器上发送消息,将消息广播给应该接收它的那些连接的客户端。然后,您可以使用上述方法在Angular代码的根范围内触发相应的事件。我不会详细了解这个,因为有很多关于使用Socket.io执行此操作的教程。

相关问题