简体   繁体   English

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

[英]How to pass URL parameters through templateUrl in AngularJS?

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. 谢谢。

Thanks guys,this is what I wanted 谢谢你们,这就是我想要的

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

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

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;
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM