【问题标题】:UI Router 1.0 state change events not workingUI Router 1.0 状态更改事件不起作用
【发布时间】:2017-04-15 08:17:34
【问题描述】:

我正在将 Angular 1.5 应用程序迁移到 Angular UI Router 1.0-beta3 以使用组件路由器。我正在监听 $rootScope 上的状态更改事件,但没有触发任何事件。

例子:

angular
    .module('app', [ 'ui.router' ])
    .config(function($stateProvider) {
        $stateProvider.state(
            'dashboard',
            {
                url: '/dashboard',
                component: 'dashboardComponent',
                resolve: {
                    enabled: function() { throw new Error('NOT ENABLED'); }
                }
            });
    })
    .run(function($rootScope) {
        $rootScope.$on('$stateChangeError', function() {
            // Never called
        });
    });

组件路由是否支持状态变化事件?

【问题讨论】:

    标签: javascript angularjs angular-ui-router angular-components


    【解决方案1】:

    组件路由支持状态变化事件,但 UI Router 在 v1.0 中处理事件的方式不同。

    试试这个:

    ...
    
    .run(function($transitions) {
        $transitions.onError({}, function() {
            // Works like a charm
        });
    });
    

    查看docs了解更多信息

    【讨论】:

    • 如果你仍然想使用 rootScope 事件那么你需要包含 stateEvents.js 并将其添加为依赖 ui.router.state.events
    • @fernando 好点。但是,状态事件在 ui-router v. 1.0 中已被弃用。 The docs 建议改用过渡挂钩。
    • 是的,它也非常强大。我们在当前项目中大量使用状态事件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多