我正在尝试使用routeParams从url中获取一个查询值。
我在html页面中包含了角路径:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-route.min.js"></script>在我的应用程序中包括Ng-Route:
var app = angular.module('app', [
'ngRoute',
'reportController'
]);并将我的控制器设置如下:
var reportController = angular.module('reportController', []);
reportController.controller('CandidateCtrl', ['$scope', '$routeParams',
function($scope, $routeParams) {
console.log(JSON.stringify($routeParams, null, 4));
$scope.person = $routeParams.person;
}]);但是,当我访问以下URL时,routeParams是一个空对象:{}。
http://localhost:8080/report?person=afe75cc7-fa61-41b3-871d-119691cbe5ad我做错了什么?
编辑:
我已经配置了可能的路由--我的routeParams对象仍然是空的。我试过:
famaApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/report:person', {templateUrl: 'Report.html', controller: 'CandidateCtrl'});
}]);和
when('/report?person', {templateUrl: 'Report.html', controller: 'CandidateCtrl'});发布于 2015-09-10 16:06:14
而不是像http://localhost:8080/report?person=afe75cc7-fa61-41b3-871d-119691cbe5ad那样访问
尝试像http://localhost:8080/#/report/afe75cc7-fa61-41b3-871d-119691cbe5ad一样访问它
您将在$route.Params中获得guid
发布于 2015-09-10 15:59:03
您必须设置您的路线,以接收您想要的参数与$routeProvider。
示例:
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
}]);那你就可以
http://localhost:8080/afe75cc7-fa61-41b3-871d-119691cbe5a$routeProvider将调用您的html和控制器,正如您在.when(‘/:person’)中看到的那样。然后,您可以尝试访问您的$routeParams,您将有您的人,$routeParams.person。
https://stackoverflow.com/questions/32506316
复制相似问题