Gowri Shankar T
Gowri Shankar T

Reputation: 35

Dynamic URL router in angular js with Ionic

My Code:

.config(function($stateProvider, $urlRouterProvider,$httpProvider) {
    $stateProvider  
    .state('app', {
        url: "/app",
        abstract: true,
        templateUrl: "templates/menu.html",
        controller: 'AppCtrl'
    })

    .state('app.englishstrokestutorials', {     
        url: "/englishstrokestutorials/:categoryId/:sourceId/:activityId/:tutorId/",
        views: {
            'menuContent': {                
                templateUrl: "templates/Beginner/unit_1/conversation/activity_1.html",
                controller: 'Englishstrokes_tutorials'
            }
        }       
    })
});

How to pass the template URL page dynamically according to the (categoryId/:sourceId/:activityId/:tutorId/) category Id,Source Id,Activityid,Tutorid all are auto increment value(dynamic). but controller is constant.

Kindly help me.

Upvotes: 0

Views: 1650

Answers (1)

GuillaumeP
GuillaumeP

Reputation: 524

You can dynamically load template according to the url params :

$stateProvider.state('app.englishstrokestutorials', {     
    url: "/myUrl/:category",
    views: {
        'menuContent': {

            templateUrl: function ($stateParams){
                // Here you can access to the url params with $stateParams
                // Just return the right url template according to the params
                if ($stateParams.category == 'foo') {
                    return 'yourtemplate1.html'
                }
                else (if $stateParams.category == 'bar') {
                    return 'yourtemplate2.html'
                }
            },
            controller: 'MyController'
        }
    }       
})

Upvotes: 2

Related Questions