1

我需要使用 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,我正在考虑检查参数的长度等......但也许有人做了类似的更好的事情。

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

非常感谢!!

4

1 回答 1

0

好的,如果可以帮助某人,我将最终发布我所做的事情。

我将上面的代码替换为:

$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

        ]})
于 2013-10-30T11:50:40.170 回答