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

0

I would like to know how I can do to call a function that is in another controller example:

I am in the 'Cadastro' view and after registering I am redirected to view 'Home' in controller of Home I have a function that receives a parameter.

Can you call the Home function by passing a parameter while 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 
  }

})
    
asked by anonymous 25.01.2016 / 04:58

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

    
16.02.2016 / 21:26
0

Configure 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 navigate to this state by passing the -

$state.go('tab.home/teste');
    
25.01.2016 / 06:18