Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a variable to $scope in $routeProvider

I need to pass a variable to $scope in $routeProvider. Code is following:

.when('/:zone/edit/:id', {
      templateUrl: 'views/edit.html',
      controller: 'EditSet'
    })
    .when('/articles/edit/:id', {
      templateUrl: 'views/edit-article.html',
      controller: 'EditSet',
      params: {zone: 'articles'}
    })

To get param I use $scope.params.zone, which works in first case (:zone) and does not work in second case.

What should I do in this case?

like image 369
Anton I Avatar asked Jul 04 '14 17:07

Anton I


1 Answers

So I think you might be better off using the function support in templateUrl to achieve this:

.when('/:zone/edit/:id', {
    templateUrl: function(params) {
        if (params.zone == "article") {
            return 'views/edit-article.html';
        }

        return 'views/edit.html';   
    },
   controller: 'EditSet'
})
like image 88
Dean Ward Avatar answered Oct 02 '22 22:10

Dean Ward