繁体   English   中英

如何从Angular JS的URL中使用$ 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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM