如何在URL中传递和读取多个参数 - angularjs

时间:2015-03-10 10:20:37

标签: angularjs

各自的控制器有2个视图。 链接在View1中。单击此链接应加载View2并读取参数。 这是我尝试过但警报说 - undefined。 View2可以使用/不使用params加载 - 每种情况都有不同的工作流程。

View1.html:

<div ng-controller="view1ctrl">
<a href="#/view2/pqid/775/cid/4" >Link1</a>
</div>

app.js:

var app = angular.module('app', ['ngRoute']);
app.config(['$routeProvider', function ($routeProvider) {
    $routeProvider
        .when('/view1', {
        templateUrl: 'App/views/view1.html',
        controller: 'view1ctrl'
        })
        .when('/view2', {
            templateUrl: 'App/views/view2.html',
            controller: 'view2ctrl'
        })
        .when('/view2/:pqid/:cid', {
            templateUrl: 'App/views/view2.html',
            controller: 'view2ctrl'
        })            
        .otherwise({
        redirectTo: '/view1'
        });
}]);

view2ctrl.js:

app.controller("view2ctrl", ['$scope','$routeParams',function ($scope, $routeParams) {
    var init = function () {
        alert($routeParams.pqid);
}
init();
}]);

1 个答案:

答案 0 :(得分:2)

你快到了:

.when('/view2/:pqid/:cid'

以这种格式映射到网址:

view2/1234/4567

1234是pqid,4567是cid。

所以你的路由,我认为是有效的,只需将链接更改为#/ view2 / 775/4。

或者,如果您想保留相同的链接,请将路由更改为:

#/view2/pqid/:pqid/cid/:cid
相关问题