Pass object between views

Asked

Viewed 1,198 times

1

How could I pass a JSON object between views:

    angular.module('app')
    .controller('MeuControle', function($scope) {
      $scope.listaPessoas = [
        {
          nome: "Patrick",
          idade: 19
        },
        {
          nome: "Joao",
          idade: 17
        },
        {
          nome: "Maria",
          idade: 20
        }
      ];

      $scope.clicada = function(pessoa) {
        $scope.pessoaSelecionada = pessoa;
        $location.path("app/pessoaDetalhe");
      };
    })

listing people:

    <ion-view hide-back-button="true" view-title="Listando Pessoas">
      <ion-content class="padding">
        <h1>Inicio</h1>
          <button ng-repeat="pessoa in listaPessoas" ng-click="clicada(pessoa)">
            {{pessoa.nome}}
          </button>
      </ion-content>
    </ion-view>

However the $Cope.person variableSelected arrives in the other empty view (app/person), both use the same controller:

    <ion-view hide-back-button="true" view-title="Detalhes">
      <ion-content class="padding">
        <h1>Nome {{pessoaSelecionada.nome}} idade {{pessoaSelecionada.idade}} </h1>


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

If I leave it fixed $Scope.personSelected = {name: "so-and-so", age: 20} the values pass, dynamically no.

  • Depending on your application you can try this: $rootScope.pessoaSelecionada = pessoa;

  • try to initialize the variable $scope.pessoaSelecionada with null and tell me if it worked

2 answers

0


Try separating into two different controllers as they are different views. As much as the controller is the same, if you have an "ng-controller='Meucontrole'" in two different views, the $Scope of the two will be different. Then I guess you won’t be able to do what you want.

If you are using "ngRoute", you can pass the values by parameters from one controller to the other using "$routeParams" (however you will not be able to pass the object, only value per value).

Every time you give a "$Location.path", the controller runs again, so I don’t think I can. When you pass fixed, you should be passing the values outside of the "$Scope.clicked" function, right? For this reason it should work.

Example:

.controller('MeuControle', function($scope, $routeParams) {
  if ($routeParams.nome)
     alert($routeParams.nome);
  ...
  $scope.clicada = function(pessoa) {
    $scope.pessoaSelecionada = pessoa;
    $location.path("app/pessoaDetalhe?nome=" + pessoa.nome + "&idade=" + pessoa.idade);
  };
}
  • 1

    Thank you so much, I did it that way, it’s even more elegant!

  • Glad I could help. Thanks for the feedback.

0

I couldn’t quite understand the structure of your views, but if they are in a single file or if they are using ng-include, you can use a controller only for more than one view, try to pass an alias at the controller’s instantiation, otherwise, if you use an ng-controller in each view, the alternative suggested by Alexandre is as appropriate as can be seen in this link: Use of controller in different views

Browser other questions tagged

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