如何使用 URL 在 angular js 中使用 $routeParams 获取参数值?

How to get parameters value using $routeParams from URL in angular js?

我正在研究 Angularjs,我遇到了一个奇怪的问题,我无法使用 $routeParams 从 URL 获取参数值..

var MyApp = angular.module('testAngularApp', ['ngRoute','ngResource'])
    .config(['$routeProvider',
        function ($routeProvider) {
            $routeProvider.
                when('/', {
                when('/updateUser/:id', {
                    templateUrl: 'Pages/User/Edit.html',
                    controller: 'UpdateUserCtrl'
                })
        }]);

控制器

angular.module('testAngularApp').
    controller('UpdateUserCtrl', ['$scope', 'updateUserService', '$http','$route', '$routeParams', function ($scope, updateUserService, $routeParams,$route, $http) {
        $scope.testV = "Update User Ctrl";
        console.log("hello");
        $scope.params1 = $routeParams.id; 
        console.log($routeParams.id ); //i am getting undefined
        console.log($scope.params1); //i am getting undefined
        var check = $routeParams['id'];
        console.log(check); //i am getting undefined
    }]);

请告诉我如何从 URL 获取值,我已经完成了这些解决方案,但它对我不起作用.. link1 link2

我认为 service 的顺序是错误的。尝试如下更改

controller('UpdateUserCtrl', ['$scope', 'updateUserService','$route','$http' '$routeParams',
                     function ($scope, updateUserService,$route, $http, $routeParams)
$routeParams

将从URL获取参数列表到您的控制器

你定义的函数定义有误。它的定义顺序应与我们为局部变量创建的顺序相同。

controller('UpdateUserCtrl', 
['$scope', 'updateUserService', '$http','$route','$routeParams', 
function ($scope, updateUserService, $http,$route,$routeParams ) {
// your Script goes Here.
console.log($routeParams.id);
}