我正在尝试使用 $routeParams 获取 URL 参数:
var myApp = angular.module('myApp', [
'ngRoute',
'appControllers',
'appFilters',
'appServices'
]).config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/admin/organisations/:organisation_id', {
controller: 'UpdateOrganisationCtrl'
});
}]);
在我的 Controller 中:
appControllers.controller('UpdateOrganisationCtrl', ['$rootScope', '$scope', '$http', '$window', '$routeParams',
function($rootScope, $scope, $http, $window, $routeParams) {
console.log($routeParams.organisation_id);
}]);
但是我打印 undefined
因为 $routeParams
是 {}
有什么想法吗?
最佳答案
您尝试访问的网址应该是
localhost:3000/#/admin/organisations/56cde4bf911747ea200d5a63
ngRoute
将使您的应用程序成为单页应用程序,其中路由器 URL 的相关部分将位于 #
之后(漂亮地称为 hashbang
)。
// Given:
// URL: http://server.com/index.html#/Chapter/1/Section/2?search=moby
// Route: /Chapter/:chapterId/Section/:sectionId
//
// Then
$routeParams ==> {chapterId:'1', sectionId:'2', search:'moby'}
但是,如果您使用 HTML5Mode 进行 Angular 的 $location 服务,则您提供的 url 可以工作。
请参阅这些链接以获取更多信息:
关于javascript - 路由参数为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35727531/