$state.go with parameter, passing parameters

Im developing Ionic cordova application for the first time watching tutorials. im now almost developed my app. and i got this problem i cant solve. that i want to pass variable from one funtion to another function.

Here is the Main code that generate the Value

$scope.ajaxLogin = function(){
    var fn = document.getElementById("username").value;
    var pw = document.getElementById("password").value;
    url: "myurl", 
    method: "POST",
    data: { username: fn, password: pw },
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).success(function(data, status, headers, config) {

    if(status == 200) {

    var loginid = 1; //  i want to make available in other functions as well. 
          //otherfunction(loginid) i tried something like this. but i dont want to call the function here. i want to call it only if button is clicked. when button is Onclick that loginid should be in that function as well
        if(loginid != 0){

          $state.go('app.profile',{mainid:loginid}); // i also now trying like this but dont know how to catch this parameter in the other function. can anyone send me link to refer it. i cant find online

  error(function(data, status, headers, config) {
   // called asynchronously if an error occurs
   // or server returns response with an error status.


i want to simple do somthing like this

.controller('AppCtrl', function($scope, $ionicPopup, $timeout, $ionicModal, $state, $http, $ionicLoading) {

        var loginid;// globally Defined
        $scope.ajaxLogin = function(){
            loginid = 1;
        $scope.myInfo = function(){

then i can click button anytime and myInfo function gives 1 instead of undefined

Please some one can help me. if you want i can describe how my app works. its simple app that retrive id after user login to the application. loginid will be the id in database table. and it will be ther reference to other functions to retreive the data

You could use $rootScope to set loggedin globally.
Also good practice would be to setup promise in your request or use events instrad.

For your problem just $rootScope should solve it!

1 Like

I don’t think your issue is linked to $state.go in any way. If you have a variable you want to use in multiple functions, you should define it on the scope :

$scope.loginid = 1;

And then it will be available in both functions (still as $scope.loginid, not loginid alone).

Now, if you want to check if a variable exists before going to a state (like if the user is logged before displaying the profile), you should take a look at Resolve, in which you could return a promise while checking if user is logged.

Good luck !

1 Like

Thanks. Resolve Link is Broken. Page not Found.

Fixed link. Good luck.

You just saved my life. i didnt sleep 2 days well.