AngularJS - redirect page to another page in angular js when trying to get parameter from url

Here's my controller code

.when('/showprofile/:UserID', {
      templateUrl: 'resources/views/layout/showprofile.php',
      controller: 'ShowOrderController',
     })

      

I am passing parameter by url.

I am trying to access this page by url directly like this

http://192.168.1.58/myapp/#/showprofile/8

But it redirects me to

http://192.168.1.58/myapp/#/showprofile/:UserID

How can I get the url value in my opinion?

Here is my app.js and here is my authCtrl.js

+3


source to share


4 answers


Try this in your controller, it will return an object based on the url value, then we can get a respected value like this



//it will return the object
console.log($routeParams);

//get the specific url value like this
console.log($routeParams.UserID);
   or
console.log($route.current.params.UserID);
      

Run codeHide result


+3


source


Yes it is possible, but you have to enter $state

into your controller and get

if you are using $ state means



console.log($state.params.userID);

      

+1


source


Try it...

   var sampleApp = angular.module('sampleApp', []);

sampleApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/ShowOrder/:orderId', {
    templateUrl: 'templates/show_order.html',
    controller: 'ShowOrderController'
      });
}]);


sampleApp.controller('ShowOrderController', function($scope, $routeParams) {

    $scope.order_id = $routeParams.orderId;

});

      

+1


source


That's right, you have something like this in app.js:

.when('/showprofile/:UserID', {
  templateUrl: 'resources/views/layout/showprofile.php',
  controller: 'authCtrl',
 })

      

This means that authCtrl has been assigned to this view. So, you need to inject routeParams into authCtrl (remember about dependency injection in javascript):

    app.controller('authCtrl', ['$scope','$rootScope','$routeParams','$location', '$http', 'Data', function ($scope, $rootScope, $routeParams, $location, $http, Data) {
    $scope.myVar = $routeParams.UserID;
    console.log('UserID: ',$scope.myVar);
    /* (...) */
}]);

      

Could you tell me if this change logs the UserID in the console? Or empty? If it registers, everything works fine and you can use a service to pass this variable between different controllers.

0


source







All Articles