如何通过AngularJS中的templateUrl传递URL参数?

时间:2022-03-02 19:43:10

Trying to learn AngulareJS got stuck with this.

试图学习AngulareJS一直坚持这一点。

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.

请问任何人请告诉我这样做的正确方法是什么?谢谢。

2 个解决方案

#1


12  

Thanks guys,this is what I wanted

谢谢你们,这就是我想要的

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

templateUrl: function(params) {
    return '/sort/' + params.expressionId +'/'+ params.typeId ;
},
controller: 'tasksController'
});

#2


1  

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

可能你正在寻找$ 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;
}]);

#1


12  

Thanks guys,this is what I wanted

谢谢你们,这就是我想要的

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

templateUrl: function(params) {
    return '/sort/' + params.expressionId +'/'+ params.typeId ;
},
controller: 'tasksController'
});

#2


1  

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

可能你正在寻找$ 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;
}]);