【问题标题】:One segment route for two different operations用于两种不同操作的一段路线
【发布时间】:2013-10-30 19:50:40
【问题描述】:

我需要使用 angular-ui 路由器将一个段的路由解析为不同的行为。

例如我这样做:

$stateProvider
    .state('parent', {
        url: '/:route'
        template: '<div ui-view/>'
        controller: ['$scope', '$state', '$stateParams', ($scope, $state, $stateParams)->

            # I get the type of route that the user is visiting checking server side and returning its type..
            switch typeOfRoute
                when "user"
                    $state.transitionTo('parent.user.profile')
                when "place"
                    $state.transitionTo('parent.place.page')
                else
                    alert("bad route")
        ]
        })
    .state('parent.user', {
        abstract: true
        template: '<div ui-view/>'
        })

    .state('parent.user.profile', {
        template: '<h1>This is a profile</h1>'
        controller: ['$scope', '$state', '$stateParams', ($scope, $state, $stateParams)->

            # Here happens all about the user
        ]})

    .state('parent.place', {
        abstract: true
        template: '<div ui-view/>'
        })

    .state('parent.place.page', {
        template: '<h1>This is a place</h1>'
        controller: ['$scope', '$state', '$stateParams', ($scope, $state, $stateParams)->

            # Here happens all about the place

        ]})

如果有人访问,使用上面的示例:

/Johndoe -> 返回该路由是一个用户并转换到 parent.user.profile /Newyork -> 返回该路线是一个地点并转换到 parent.place.page

使用这种方法的问题是,当我编写解决 /Johndoe/likes 的路线时,浏览器再次转到 /Johndoe,我正在考虑检查参数的长度等......但也许有人做了类似的事情并且更好。

有人试图构建类似的东西吗?我卡在这一点上。

非常感谢!!

【问题讨论】:

    标签: html angularjs angular-ui angular-ui-router


    【解决方案1】:

    好的,如果对某人有帮助,我将最后发布我所做的事情。

    我将上面的代码替换为:

    $urlRouterProvider.when('/:route', ['$match', '$stateParams', 'urlService', '$state', ($match, $stateParams, urlService, $state)->
    
        if $match.route and $match.route.length > 0 # you can make more complex route checking here.
    
            # here transition to the desired type route resolved server side 
            switch typeOfRoute
               #the same for checking.. 
                when "user"
                    $state.transitionTo('user.profile', $match, false)
                when "place"
                    $state.transitionTo('place.page', $match, false)
        else
            return false    
    
    
    $stateProvider
        .state('user', {
            abstract: true
            template: '<div ui-view/>'
            })
    
        .state('user.profile', {
            template: '<h1>This is a profile</h1>'
            controller: ['$scope', '$state', '$stateParams', ($scope, $state, $stateParams)->
    
                # Here happens all about the user
            ]})
    
        .state('place', {
            abstract: true
            template: '<div ui-view/>'
            })
    
        .state('place.page', {
            template: '<h1>This is a place</h1>'
            controller: ['$scope', '$state', '$stateParams', ($scope, $state, $stateParams)->
    
                # Here happens all about the place
    
            ]})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 2020-12-06
      • 1970-01-01
      • 2012-07-21
      相关资源
      最近更新 更多