angular-route-segment
angular-route-segment copied to clipboard
How set custom params?
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?
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
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.
@peunoir thanks for routeSegmentChange
. Haven't crossed my mind to look for an event in the code :)