[英]How to get a url param in AngularJS
Any idea why $routeParams
is logging undefined
? 知道$routeParams
为什么undefined
日志记录吗? I would expect it to log an id. 我希望它记录一个ID。
var app = angular.module('Example', ['ngResource', 'ngRoute']);
app.config([
'$routeProvider', function($routeProvider) {
return $routeProvider.when('/entries/:entryId', {
controller: 'EntryController'
});
}
]);
app.controller('EntryController', [
'$scope', '$routeParams', '$http', function($scope, $routeParams, $http) {
$http.get("/entries/" + $routeParams.entryId + ".json").success(function(data) {
return $scope.phone = data;
});
// This logs undefined.
console.log($routeParams.entryId);
}
]);
Current URL is http://localhost:3000/entries/5383a2f44d6f6816d7020000
当前URL是http://localhost:3000/entries/5383a2f44d6f6816d7020000
I do NOT use return
on my route configuration and I failed to find the templateUrl
in your sample: 我在路由配置中不使用return
,但是在示例中找不到templateUrl
:
$routeProvider.when('/entries/:entryId', {
templateUrl: 'sample.html',
controller: 'MyController'
});
A complete sample will be like this: 完整的示例如下:
angular.module('demo', ['ngRoute'])
.controller('MainController', function($scope, $route, $routeParams, $location) {
})
.controller('SampleController', function($scope, $routeParams) {
$scope.name = "SampleController";
$scope.params = $routeParams;
})
.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/entries/:id', {
templateUrl: 'sample.html',
controller: 'SampleController'
});
// configure html5 to get links working on jsfiddle
$locationProvider.html5Mode(true);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.