简体   繁体   English

角$ routeParams空对象

[英]Angular $routeParams empty object

I'm trying to get a query value from my url using angular's routeParams. 我正在尝试使用angular的routeParams从我的URL中获取查询值。

I've included angular-route in my html page: 我已经在我的HTML页面中添加了angular-route:

 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-route.min.js"></script>

Included Ng-Route in my app: 我的应用程式包含Ng-Route:

var app = angular.module('app', [
    'ngRoute',
    'reportController'
]);

And set up my controller as following: 并按如下所示设置我的控制器:

var reportController = angular.module('reportController', []);
reportController.controller('CandidateCtrl', ['$scope', '$routeParams',
        function($scope, $routeParams) {
    console.log(JSON.stringify($routeParams, null, 4));
    $scope.person = $routeParams.person;
}]);

When I access the following URL, however, routeParams is an empty object: {}. 但是,当我访问以下URL时,routeParams是一个空对象:{}。

http://localhost:8080/report?person=afe75cc7-fa61-41b3-871d-119691cbe5ad

What am I doing wrong? 我究竟做错了什么?

Edit: 编辑:

I've configure the possible route - my routeParams object is still coming up null. 我已经配置了可能的路线-我的routeParams对象仍然为null。 I've tried: 我试过了:

famaApp.config(['$routeProvider', function($routeProvider) {
           $routeProvider.
            when('/report:person', {templateUrl: 'Report.html', controller: 'CandidateCtrl'});
}]);

and

when('/report?person', {templateUrl: 'Report.html', controller: 'CandidateCtrl'});

You have to set your routes to receive the arguments you want with $routeProvider.. 您必须设置路由以使用$ routeProvider接收所需的参数。

Example: 例:

app.config(['$routeProvider', '$locationProvider' function ($routeProvider, $locationProvider) {

    $routeProvider
        .when('/',
        {
            templateUrl: 'someHtmlUrl',
            controller: 'someController',
            controllerAs: 'someCtrl',
            caseInsensitiveMatch: true
        })
        .when('/:person',
        {
            templateUrl: 'someHtmlUrl',
            controller: 'someController',
            controllerAs: 'someCtrl',
            caseInsensitiveMatch: true
        })
        .otherwise(
        {
            templateUrl: 'someHtmlUrl'
        });
    $locationProvider.html5Mode(true); //Use html5Mode so your angular routes don't have #/route
}]);

Then you can just do 那你就可以做

http://localhost:8080/afe75cc7-fa61-41b3-871d-119691cbe5a

And the $routeProvider will call your html and your controller as you can see with the .when('/:person'.. and then you can try and access your $routeParams and you will have your person there, $routeParams.person. 然后$ routeProvider将调用您的html和您的控制器,如您所看到的那样,您可以使用.when('/:person'..然后您可以尝试访问$ routeParams,然后将您的人放置在$ routeParams.person中。

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

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