angular-route-segment icon indicating copy to clipboard operation
angular-route-segment copied to clipboard

How set custom params?

Open Jasur opened this issue 10 years ago • 3 comments

I've got a code which works perfectly

$routeProvider.when('/',{
        access: access.user
        ,someKey:{ss:"dd"}
    });

and when I need a check

app.run(['$rootScope', '$location', 'Auth', '$log' function ($rootScope, $location, Auth, $log ) {
    $rootScope.$on("$routeChangeStart", function (event, next, current) {
        if (!Auth.authorize(next.access)) {    //<<<--whith segment here occures an Error

            if(Auth.isLoggedIn()) {

            } else {

            }
        } else {

        } 
    });
}]);

all works well. I mean, that there is "next.access" exists which a set above.

But, with segments its not works if

$routeSegmentProvider
        .when('/', 'mainRoot')
        .segment('mainRoot',{
            controller: 'mainRootCtrl'
            ,access: access.user
        })

How Can I solve this problem?

Jasur avatar Nov 08 '13 07:11 Jasur

Temprorarily a changed function "when" like

...javascript
$routeSegmentProvider.when = function(route, name) {
        if ( typeof(name) == 'object' ) {
            $routeProvider.when(route, name);
        } else {
            $routeProvider.when(route, {segment: name});
        }
        return this;
    };
...

and the code

$routeSegmentProvider
        .when('/', { segment:'mainRoot',access: access.user } )
        .segment('mainRoot',{
            controller: 'mainRootCtrl'

        })

works for a while, but i dont think that this is good solve of the problem

Jasur avatar Nov 08 '13 07:11 Jasur

@Jasur

Hi, if you would like to solve this problem without changing the function, you can try this.

.when('/s1', 's1')
.segment('s1', {
templateUrl: 'views/home.html',
controller: 'HomeCtrl',
access: access.user
});


app.run(['$rootScope', '$location', 'Auth', '$log' function ($rootScope, $location, Auth, $log ) {
$rootScope.$on("routeSegmentChange", function (event, route) {
var access = route.segment.params.access;
if (!Auth.authorize(access)) { //<<<--within segment here occurs an Error
        if(Auth.isLoggedIn()) {

        } else {

        }
    } else {

    } 
});
}]);

This module (angular-route-segment) broadcasts a custom event, routeSegmentChange with an object that has two properties, index and segment.

I hope you find this useful.

alexunm avatar Dec 26 '13 00:12 alexunm

@peunoir thanks for routeSegmentChange. Haven't crossed my mind to look for an event in the code :)

igor10k avatar Jul 28 '14 19:07 igor10k