Pass object between views

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 $ scope.sampled variable arrives in the other empty view (app / personDetails), 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 fixed $ scope.PersonSelected = {name: "so-and-so", age: 20} values pass, not dynamically.

    
asked by anonymous 25.02.2016 / 23:20

2 answers

0

Try splitting into two different controllers since they are different views. As much as the controller is the same, if you have an "ng-controller = 'MyControl'" in two different views, the $ scope of the two will be different. So I believe you will not be able to do what you want.

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

Every time you give a "$ location.path", the controller executes again, which is why I do not think I can. When you pass fixed, you should be passing the values out of the "$ scope.click" 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);
  };
}
    
26.02.2016 / 21:43
0

You can not really 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 passing an alias in the instantiation of the controller, otherwise, if you use an ng-controller in each view, the alternative suggested by Alexandre is adequate as seen in this link: Using controller in different views

    
26.02.2016 / 22:50