gpt4 book ai didi

javascript - 如何在 AngularJS 中获取 url 参数

转载 作者:行者123 更新时间:2023-12-03 12:32:18 24 4
gpt4 key购买 nike

知道为什么$routeParams正在记录未定义吗?我希望它能记录一个 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);
}
]);

当前网址为http://localhost:3000/entries/5383a2f44d6f6816d7020000

最佳答案

我没有在路由配置中使用 return,并且在您的示例中找不到 templateUrl:

$routeProvider.when('/entries/:entryId', {
templateUrl: 'sample.html',
controller: 'MyController'
});

完整的示例如下:

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);
});

关于javascript - 如何在 AngularJS 中获取 url 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23879172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com