Search code examples
javascriptangularjscontrollerrouteparams

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


I am working on Angularjs, I am stuck in weired problem, I am not able to get parameters value from URL using $routeParams..

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

controller

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

please tell me how to get values from URL, I have went through these solutions but it didn't work for me.. link1 link2


Solution

  • I think order of services is wrong. try change as below

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