Call function in $state.go() Ionic/Angular.js

Asked

Viewed 3,348 times

0

I wonder how I can call a function that is in another controller example:

I am in the 'Register' view and after making a registration I am redirected to view 'Home' no controller from Home I have a function that receives a parameter.

You can call the Home function by passing a parameter being in Register?

Example:

.controller('HomeCtrl', function($scope, $state) {

  $scope.myFunction = function(valor) {

    alert(valor);

  }
})

.controller('AddCtrl', function($scope, $state) {

  $scope.add = function() {
    $state.go('tab.home');
    //Ir para tela Home e chamar a função myFunction('teste')
    //passando 'teste' como parãmtro 
  }

})

2 answers

1


You can do it this way:

.controller('HomeCtrl', function($scope, $state) {

    $rootScope.myFunction = function(valor) {

       alert(valor);

    }
})

.controller('AddCtrl', function($scope, $state) {

   $scope.add = function(valor) {

      $state.go('tab.home').then(function(){

          $rootScope.myFunction(valor);

      });
   }

 })

$rootScope is accessible on all controllers

  • Poxa André Vicente, I found this solution very good and practical! Thanks friend, this solves my case. Thanks!!

0

Set your route to accept an optional parameter:

$stateProvider
    .state('tab.home', {
        url: "/home/:parm",
        templateUrl: 'home.html',
        controller: function ($stateParams) {
            var parm = $stateParams.parm;
        }
    })

And to sail this way state passing the parameter -

$state.go('tab.home/teste');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.