首页 > 解决方案 > AngularJS - 什么是 $transitions?

问题描述

目前正在使用 AngularJS + NodeJS 应用程序,突然间我看到了这段代码:

    $transitions.onStart({exiting: 'orders.view'}, function(trans) {
        Socket.emit('orders:leave', {id_order: trans.params('from').id_order});
    });

$transitions帮我找出午餐吃什么。

谢谢。


答案编辑后:

对于那些最终登陆这里的人,您基本上可以在这些过渡中做任何您想做的事情;包括在退出 X 状态时做一些事情。

'use strict';

// Configuring the orders module
angular.module('orders').run(['Menus','MODULE_LIST', 'Authentication', '$transitions', 'Socket', '$templateCache',
    function(Menus, MODULE_LIST, Authentication, $transitions, Socket, $templateCache) {

        $transitions.onStart({exiting: 'orders.view'}, function(trans) {
          alert('Alert function has stopped you from going further BEEP BOOP')
          Socket.emit('orders:leave', {id_order: trans.params('from').id_order});
        });

        var stlViewPopoverHtml =
            '<div>' +
            '<img ng-src="{{url}}" height="{{height}}" width="{{width}}">' +
            '</div>';

        $templateCache.put('stl-preview-popover.html', stlViewPopoverHtml);
    }
]);

标签: angularjsangular-ui-router

解决方案


这是 UI-Router 的一部分。你可以在这里找到文档:Transition Hooks

例如$transitions.onStart,当您从一种状态移动到另一种状态时,将注册一个触发提供的函数的转换钩子。在您的示例中,它只会在退出提供的状态时触发,在您的情况下,状态是orders.view.

总结一下您提供的代码将做什么:退出orders.view状态时,Socket.emit一旦状态转换开始就会触发 。


推荐阅读