rahulbhondave
rahulbhondave

Reputation: 186

Angularjs routeprovider.when for set of conditions and one controller

I have 3 urls(blog, help, tips) for routing which will be having same controller and same html page.

rcapp.config(function($routeProvider) {

    $routeProvider

    .when('/csv-validation',{

        templateUrl:'csv-validation.html',
        controller:'csvController'
    })
    .when('/blog',{

        templateUrl:'blog_tips.html',
        controller:'blogTipsController'
    })
    .when('/tips',{

        templateUrl:'blog_tips.html',
        controller:'blogTipsController'
    })
    .when('/help',{

        templateUrl:'blog_tips.html',
        controller:'blogTipsController'
    })
});

can i do it in one .when condition for those 3 urls.

Upvotes: 0

Views: 501

Answers (1)

Duncan
Duncan

Reputation: 95672

Not a single .when, but you can put it in a loop:

rcapp.config(function($routeProvider) {

    var rp = $routeProvider

    .when('/csv-validation',{

        templateUrl:'csv-validation.html',
        controller:'csvController'
    });

    angular.forEach(['/blog', '/tips', '/help'],
       function(path) { rp = rp.when(path, {
               templateUrl:'blog_tips.html',
               controller:'blogTipsController'
           });
       });
});

Or I think you could use array.reduce:

['/blog', '/tips', '/help'].reduce(function(rp, path) {
    return rp.when(path, {
        templateUrl:'blog_tips.html',
        controller:'blogTipsController'
    });
}, rp);

Upvotes: 2

Related Questions