AngularJS - 如何从URL获取参数

时间:2016-10-19 06:48:42

标签: javascript angularjs

我试图将参数传递给AngularJS控制器。

AngularJs路线

$routeProvider.when('/post-ads/', {
            templateUrl: "ui/view/post-ads.php",
            controller: "adPostController",
            controllerAs: 'vm'
        })

        $routeProvider.when('/post-ads/:category/:subcategory', {
            templateUrl: "ui/view/ad-details.php",
            controller: "adPostController",
            controllerAs: 'vm'
        })

AdPostController

adPostController.$inject = ['ApiService', '$scope'];
    function adPostController(ApiService, $scope, $routeParams) {
        console.log("im here");
        console.log($routeParams.category, $routeParams.subcategory);
    }

HTML a href

<a href="#/post-ads/automobile/car>click</a>

错误

  

TypeError:无法读取属性&#39;类别&#39;未定义的

2 个答案:

答案 0 :(得分:0)

更改为:

adPostController.$inject = ['ApiService', '$scope', '$routeParams'];

答案 1 :(得分:0)

我想你必须改变:

adPostController.$inject = ['ApiService', '$scope'];
    function adPostController(ApiService, $scope, $routeParams) {
        console.log("im here");
        console.log($routeParams.category, $routeParams.subcategory);
    }

要:

adPostController.$inject = ['ApiService', '$scope', '$srouteParams'];
    function adPostController(ApiService, $scope, $routeParams) {
        console.log("im here");
        console.log($routeParams.category, $routeParams.subcategory);
    }