Issue with passing values through state.go

245
January 16, 2017, at 09:07 AM

I'm having trouble passing through a uid into the $state.go function.

Here is my controller:

.controller('chatCtrl', function($scope,$http,$ionicPopup,$state,$ionicHistory,$stateParams) { 
 
    $scope.data = {}; 
    $scope.session_id= sessionStorage.getItem('session_id'); 
    var sid = $scope.session_id; 
    if($scope.session_id == null){ 
        $state.go('login'); 
    } 
    else { 
        var cload = 'https://m.socialnetwk.com/home/app/chat_load.php'; 
        $http.post(cload, {id : $scope.session_id}).then(function (res){ 
            $scope.chat = res.data; 
        }); 
        $scope.chat = {}; 
        $scope.openModal = function(id) { 
            $scope.selectedId = id; 
            $state.go('chat2',{uid: $scope.selectedId}); 
 
        } 
    } 
}) 
 
 
.controller('chat2Ctrl', function($scope,$http,$ionicPopup,$state,$ionicHistory,$stateParams) { 
 
    $scope.data = {}; 
    $scope.id = {}; 
 
    $scope.session_id= sessionStorage.getItem('session_id'); 
    var sid = $scope.session_id; 
    if($scope.session_id == null){ 
        $state.go('login'); 
    } 
    else { 
        var pmloaddd = 'https://m.socialnetwk.com/home/app/upml.php'; 
        $http.post(pmloaddd, {id : $state.params.uid}).then(function (res){ 
            $scope.pmload = res.data; 
        }); 
 
 
      
 
    } 
     
})

Here is my app.js:

$stateProvider 
.state('chat2', { 
   url: '/page9', 
   params: { 
     uid: null 
   }, 
   templateUrl: 'templates/chat2.html', 
   controller: 'chat2Ctrl' 
})

Answer 1

There are two mistakes

  1. Did not add the parameter to the URL property of your state configuration
  2. And $state.params.uid is invalid.

Use the below solution.

Your app.js

$stateProvider
.state('chat2', {
   url: '/page9/:uid',             //added the parameter here
   params: {
     uid: null
   },
   templateUrl: 'templates/chat2.html',
   controller: 'chat2Ctrl'
})

Your else part of the Chat2 controller should be as below

if($scope.session_id == null){
        $state.go('login');
    }
    else {
        var pmloaddd = 'https://m.socialnetwk.com/home/app/upml.php';
        // modified the below line
        $http.post(pmloaddd, {id : $stateParams.uid}).then(function (res){
            $scope.pmload = res.data;
        });

LIVE DEMO

Answer 2

First you need to pass it with url and then you can access it like--

$stateParams.

$stateProvider
 .state('chat2', {
   url: '/page9/:uid',
     controller:'name of yout controller',
     controller: function($stateParams){
     $stateParams.uid  //*** Exists! ***//
     }
      });

then you can access like this:-

$stateparams.uid at controller.

You can also refer this link--Url routing--stateParams Service

READ ALSO
Get all files in directory json

Get all files in directory json

I want get all files json in my directory for search into it

356
Best way to put url in django and jquery ajax

Best way to put url in django and jquery ajax

I'm new in django ajaxI wrote a script which is functional

359
passing variable from jQuery ajax to nodejs

passing variable from jQuery ajax to nodejs

i am trying to pass a variable from jQuery to nodejs, but i am not getting the right results, nodejs returns [object Object]how can it return a string variable on nodejs side

444
Triggering a JQuery animate function & looping it?

Triggering a JQuery animate function & looping it?

What I'm trying to do is (presumably) relatively simple, but let down by my (admittedly poor) understanding of jquery/Javascript

284