路由参数为空

routeParams are Empty

本文关键字:参数 路由      更新时间:2023-09-26

我正在尝试使用以下$routeParams获取URL参数:

var myApp = angular.module('myApp', [
  'ngRoute',
  'appControllers',
  'appFilters',
  'appServices'
]).config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/admin/organisations/:organisation_id', {
    controller: 'UpdateOrganisationCtrl'
  });
}]);

在我的控制器中:

appControllers.controller('UpdateOrganisationCtrl', ['$rootScope', '$scope', '$http', '$window', '$routeParams',
function($rootScope, $scope, $http, $window, $routeParams) {
    console.log($routeParams.organisation_id);
}]);

但是我正在打印undefined $routeParams {} 有什么想法吗?

您尝试访问的网址应该是

本地主机:3000/#/admin/organisations/56CDE4BF911747EA200d5a63

ngRoute将使您的应用程序成为单页应用程序,其中路由器URL的相关部分将在#之后(漂亮地称为hashbang)。

看例子:

//鉴于:
网址: http://server.com/index.html#/Chapter/1/Section/2?search=moby
路由:/章/:章节 ID/节/:节

然后
$routeParams ==> {章节 ID:'1', 部分 ID:'2', 搜索:'moby'}

但是,如果您使用HTML5Mode进行角度服务$location则您提供的URL可能会起作用。

有关详细信息,请参阅这些链接:

  • 没有哈希"#"的 AngularJS 路由
  • 使用$location