UI路由器:导航到抽象父状态时重定向到子状态

时间:2016-08-05 22:08:06

标签: angularjs angular-ui-router states

在我的音乐应用中,我对应用的顶级状态进行了重复操作以创建导航链接。其中一个名为library的顶级状态是抽象的,并且具有子状态(可以使用制表符导航)。由于我使用的是ng-repeat,因此抽象状态的指令为ui-sref="library"。但是,它无法导航到这样的抽象父状态,而是需要编写ui-sref="library.albums"。由于直接来自州提供商的ng-repeat数据,我无法做到这一点。如何在"库"上设置默认子状态?这样,无论何时访问该状态,它都会重定向到孩子?

这是我的设置图: enter image description here

3 个答案:

答案 0 :(得分:10)

当我寻找相同的问题时发现了这个问题,然后我在Angular UI-Router的FAQ页面上找到了这个问题。 https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions

如何:设置默认/索引子状态

  1. 使父状态为摘要。

    $stateProvider
        .state('library', {
            url: '/library',
            abstract: true,
            template: '<ui-view></ui-view>'
        })
        .state('library.albums', {
            url: '/albums',
            template: '<h1>Albums</h1>'
        });
    
  2. 然后,如果您为子状态指定一个空URL,它将匹配父母的确切URL。但是,如果您想为子项保留单独的URL,则可以执行以下操作:

    $urlRouterProvider.when('/library', '/library/albums');
    
  3. 如果您需要更多信息,请阅读给定的GitHub WIKI URL。

答案 1 :(得分:2)

很遗憾,您无法使用ui-sref链接到抽象状态。

你可以尝试类似的东西:

$rootScope.$on('$stateChangeStart', function(event, toState, toParams){
    if(toState.name == 'library'){
        event.preventDefault();
        $state.go('library.albums', toParams);
    }
}

不是硬编码每个状态重定向,而是可以执行以下操作:

$stateProvider
    .state('library', {
        url: '/library',
        data: {
            redirect: 'library.albums'
        }
    })
    .state('library.albums', {
        url: '/albums',
        data: {
            redirect: false
        }
    });

$rootScope.$on('$stateChangeStart', function(event, toState, toParams){
    if(toState.data && toState.data.redirect){
        event.preventDefault();
        $state.go(toState.data.redirect, toParams);
    }
}

答案 2 :(得分:0)

在ui-router的V1.0中,添加了状态参数render() { const user = this.props.user const man = this.props.man // etc } (请参见ui-router docs)。这可以让您完成所需的工作。

在您的示例中,您不会使用df['Date']=pd.to_datetime(df['Date']) #### Converts Date column to Python Datetime df['daysoffset'] = df['Date'].apply(lambda x: x.weekday()) #### Return the day of the week as an integer, where Monday is 0 and Sunday is 6. df['week_start'] = df.apply(lambda x: x['Date'].date()-timedelta(days=x['daysoffset']), axis=1) #### x.['Date'].date() removes timestamp and considers only Date #### the line assigns date corresponding to last Monday to column 'week_start'. df.groupby('week_start').apply(lambda v: stats.linregress(v.Total_Sales,v.Products) [0]).reset_index() ,而是会重定向到redirectTo状态。

abstract: true

与此有关,您可以使用library.albums。这非常有用,因为$stateProvider .state('library', { url: '/library', redirectTo: 'library.albums', template: '<ui-view></ui-view>', }) .state('library.albums', { url: '/albums', template: '<h1>Albums</h1>' }); 现在可以使用。

相关问题