Pass the result of a function in controller A to controller B


#1

Hi !

I try to build my very first App with Ionic/AngularJS but I have a problem. I can’t pass the result of my function to another controller.
I followed some tutorials to pass an input content through controllers with services and it works but I can’t find any ressources to understand how to pass the result of a function because I can’t assign it to ng-model.

I have this in controller A :

<ion-item class="item-avatar">
  <img src="https://s3.amazonaws.com/ionic-io-static/la4KW6wSnuDjpLFs9WHm_ic_home_blue_48px.svg">
  <h2>{{getTotalActionsEau()}} </h2>
  <p>{{getTotalPointsEau()}}</p>
</ion-item>

I’d like to pass {{getTotalActionsEau()}} dynamically in controller B but how can I do it ? Thanks for the answer !


#2

Since it’s your first app, I think you should start learning on the lastest ionic version.

I’d help you through if you need. :slight_smile:


#3

Good advice but I made everything with Ionic Creator. My app is almost done, I just need to pass some variables between controllers and add some content. I find more tutorials about angularJS also and I read Ionic team won’t drop Ionic 1 so I’d like to understand how to pass the result of my function with Ionic 1.
But I’ll switch to the latest ionic version soon. Maybe when it’ll be possible to export project from Ionic Creator to the latest version of Ionic.


#4

That sounds like a hard thing to do. A lot has changed.

Would you mind sharing the pieces of code your are working on?
I started with IONIC 1 but it’s been a while since I played with that.


#5

Ionic team says it will be possible to create an Ionic v2 project in Ionic creator so I’ll switch at this time. For the moment, I find easier to use the ionic creator and implement some directive. I don’t have complex projects in mind so I just need to learn some little things. Thanks for your help !

For the example, I cut the code.

I have two controllers : “maisonCtrl” and “pourLEnvironnementCtrl”. In "maisonCtrl, I have a function that calculate a sum of points and I put the result in an item. I’d like to pass the result in “pourLEnvironnementCtrl”.

.controller('maisonCtrl', ['$scope', '$stateParams', '$state', '$ionicListDelegate', 'data', // The following is the constructor function for this page's controller. See https://docs.angularjs.org/guide/controller

function ($scope, $stateParams, $state, $ionicListDelegate, data) {

$scope.getTotalActionsEau = function(){
            var total = 0;
            for(var i = 0; i < $scope.items.length; i++){
                var item = $scope.items[i];
                total += item.action;
                }
            if (total > 1) {
                return total + " actions";
            }
            else {
                return total + " action";
            }
            }; 

.controller('pourLEnvironnementCtrl', ['$scope', '$stateParams', 'data', // The following is the constructor function for this page's controller. See https://docs.angularjs.org/guide/controller
function ($scope, $stateParams, data) {
}])

The item in “maisonCtrl”

<ion-item class="item-avatar" id="maison-list-item102">
          <img src="img/la4KW6wSnuDjpLFs9WHm_ic_home_blue_48px.svg">
          <h2>{{getTotalActionsEau()}} </h2>
          <p>{{getTotalPointsEau()}}</p>
        </ion-item>

The item in pourLEnvironnementCtrl :

<ion-item class="item-avatar" id="pourLEnvironnement-list-item127" ng-controller="secondCtrl">
          <img src="img/CvW94CQaSguMOL2DDhOw_stats-bars-Copie.png">
          <h2></h2>
          <p></p>
        </ion-item>

#6

I guess you can call it like this from pourLEnvironnementCtrl:

angular.element($('#maison-list-item102')).scope().getTotalActionsEau();

Take a look at my Ionic V1 project from the course I took on Coursera for future reference.


#7

Thanks a lot for the help !

I dit that but it doesn’t work :

.controller('pourLEnvironnementCtrl', ['$scope', '$stateParams', 'data', // The following is the constructor function for this page's controller. See https://docs.angularjs.org/guide/controller
function ($scope, $stateParams) {
    $scope.secondCtrl = function(){
        $scope.test = angular.element($('#maison-list-item102')).scope().getTotalActionsEau();
    };

}])
ion-item class="item-avatar" id="pourLEnvironnement-list-item127" ng-controller="secondCtrl">
          <img src="img/CvW94CQaSguMOL2DDhOw_stats-bars-Copie.png">
          <h2></h2>
          <p>{{test}}</p>
        </ion-item>

Thank you for the help !