Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass URL parameters through templateUrl in AngularJS?

Trying to learn AngulareJS got stuck with this.

This is the code :

app.config(function ($routeProvider){
$routeProvider
.when('/',
{       
    templateUrl: '/sort',
    controller : 'tasksController'      
})  
.when('/expression/:expressionId/type/:typeId',
{   
    templateUrl: '/sort/'+:expressionId +'/'+ :typeId,
    controller : 'tasksController'  
})});

This is obviously wrong.

Can any one please tell me what is the correct way to do this? Thanks.

like image 808
user727728 Avatar asked May 17 '14 16:05

user727728


2 Answers

Thanks guys,this is what I wanted

.when('/expression/:expressionId/type/:typeId', {

templateUrl: function(params) {
    return '/sort/' + params.expressionId +'/'+ params.typeId ;
},
controller: 'tasksController'
});
like image 104
user727728 Avatar answered Oct 05 '22 01:10

user727728


Probably you are looking for $routeparams https://docs.angularjs.org/api/ngRoute/service/$routeParams.

you can do something like below:

app.config(function ($routeProvider){
  $routeProvider
    .when('/',{
        templateUrl: '/sort',
        controller : 'tasksController'
    })
    .when('/expression/:expressionId/type/:typeId', {
        templateUrl: '/sort',
        controller : 'tasksController'
    })
});

app.controller('tasksController', ['$scope', '$routeparams', function($scope, $routeparams) {
    var expressionId = $routeparams.expressionId
        , typeId = $routeparams.typeId;
}]);
like image 39
ashu Avatar answered Oct 05 '22 03:10

ashu