简体   繁体   中英

AngularJS - how to get the parameter from URL

I'm trying to pass the parameters to the AngularJS controller.

AngularJs Routes

$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>

Error

TypeError: Cannot read property 'category' of undefined

更改为此:

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

I guess your have to change:

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

To:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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