I have a need to create dynamic routing and I am unable to find anything that explains how I could do it..
If my route provider looks like this:
app.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl:'/index.php',
controller: 'mainCtrl'
})
.when('/search/:tags', {
templateUrl:'/search/index.html',
controller: 'searchCtrl'
})
.when('/path/one/', {
templateUrl:'/test/two/partial.html',
controller: 'testTwoCtrl'
})
.when('/path/one/:valueone', {
templateUrl:'/mickey/mouse/partial.html',
controller: 'MickeyMouseCtrl'
})
.otherwise({
redirectTo: '/404.html'
})
});
How can I generate it dynamically from a json object that holds required information,, something like this:
[
{
"when": "/",
"templateUrl": "/index.php",
"controller": "mainCtrl"
},
{
"when": "/search/:tags",
"templateUrl": "/search/index.html",
"controller": "searchCtrl"
},
{
"when": "/path/one/",
"templateUrl": "/test/two/partial.html",
"controller": "testTwoCtrl"
},
{
"when": "/path/one/:valueone",
"templateUrl": "/mickey/mouse/partial.html",
"controller": "MickeyMouseCtrl"
}
]
I also need to load this information in an XHR request. I suppose I am looking for something to this tune.. Is this viable, possible, doable?
app.config(function($routeProvider, $http) {
$http.get('routingdata.json').success(function(data) {
for ( key in data ) {
$routeProvider.when( data[key].when, {
templateUrl:data[key].templateUrl,
controller: data[key].controller
})
}
$routeProvider.otherwise({
redirectTo: '/'
})
}
});