xenurs
xenurs

Reputation: 449

Send data from controller to an other controller

I'm new in AngularJs. I retrieve data(from json array) on my first page, and what I want to do is when I click on one item of my list it sends me on my second page which display the current id, date, amount and category. But I don't know how to do to display the current id, date, amount and category.

Here is my controller :

.controller('RegistreCtrl', function ($scope, $stateParams,factotransaction,$state) {
    console.log("coucou");
    var mytoken = sessionStorage.getItem('token');
    factotransaction.send(mytoken).then(function(conf){
        console.log(conf);
        $scope.datab = conf.data;
    })

    $scope.operation = function(){
        $state.go('app.operation');
    }       
})


.controller('OperationCtrl', function ($scope, $stateParams,factotransaction) {

     var mytoken = sessionStorage.getItem('token');
     factotransaction.send(mytoken).then(function(conf){
         console.log(conf);
         $scope.datab = conf.data;
     })     
})

and my view for first page:

<ion-view view-title="Registre">
  <ion-content>
    <h1>Registre</h1>
    <ul class="list" ng-repeat="item in datab track by $index">

      <li class="item" ng-repeat="mytitle in item.assignation" ng-click="operation()">
        {{item.date | myDate}}--

          {{mytitle.category}}--{{mytitle.amount}}€
      </li>
    </ul>
  </ion-content>

</ion-view>

and view of second page (almost empty for now):

    <ion-view view-title="Registre">
        <ion-content>
            <h1>Operation</h1>

            <div class="card">

            </div>
        </ion-content>
    </ion-view>

Do you have any idea of how can I manage that?

Upvotes: 0

Views: 41

Answers (1)

z.a.
z.a.

Reputation: 2777

<ion-view view-title="Registre">
  <ion-content>
    <h1>Registre</h1>
    <ul class="list" ng-repeat="item in datab track by $index">

you can try to pass some sort of indicator with the operation function, id maybe?

      <li class="item" ng-repeat="mytitle in item.assignation" ng-click="operation(item.id)">
        {{item.date | myDate}}--

          {{mytitle.category}}--{{mytitle.amount}}€
      </li>
    </ul>
  </ion-content>

</ion-view>

then here,

$scope.operation = function(id){
                    $state.go('app.operation', { id: id });

            }

not sure about the syntax, but check please. so now you have an id you can play with. in the controller,

    .controller('OperationCtrl', function ($scope, $stateParams,factotransaction) {

                var mytoken = sessionStorage.getItem('token');
                factotransaction.send(mytoken).then(function(conf){
                    console.log(conf);
                    conf.data.forEach(function(item){

                  if($stateParams.id === item.id) {
                  $scope.item = item;
}
});
                });

            })

then just use that $scope.item in your template code

Upvotes: 1

Related Questions