How to pass data from a selected item on an ng-repeat to another view

1

Good morning, Srs.,

How can I pass data from an item that has been selected within a ng-repeat list to another view?

Here is the code from the first view:

                <div ng-repeat="jogador in dadosjogador | filter:{xid:equipe.jogador}" >
                    <div class="item-avatar item-button-right">
                      <img src="img/user1.jpg">
                      <h2>{{jogador.nome}}</h2>
                      <form action="voto.html" class="urna">
                          <input type="submit" value="Votar" />
                      </form>                      
                    </div>
                </div>

Below is the code from the second view:

  <body ng-app="starter" ng-controller="equipecontrol">

  <ion-header-bar class="bar-calm" align-title="center">
    <button class="button button-icon icon ion-ios-arrow-back ng" ng-click="goback()"></button>
    <h1 class="title">{{jogador.nome}}</h1>
    <button class="button button-icon icon ion-navicon" ></button>
  </ion-header-bar>
  </body>

In this second view I used {{jogador.nome}} but the player name is not displayed.

I have tried to use a $rootScope._nomejogador variable, I declare the same in the controller equipecontrol, which is common to both views, but I also do not know how to do: $rootScope._nomejogador = {{jogador.nome}} the moment the item is selected and call the other view. / p>

Someone would have an idea how I can resolve this issue

    
asked by anonymous 03.06.2017 / 12:16

2 answers

1

Whenever you want to share information between parts of your Angular application, use Services or Factories .

The example below demonstrates the use of a service to share player data, as well as a primitive voting mechanism:

ClickRuntoseeitworking:

angular.module('myApp', [])
.service('jogadorService', function(){

    // Note que os dados dos jogadores são centralizados no service - 
    // assim, sempre que você modificar esta lista, todos as referências 
    // em todos os módulos consumidores serão atualizadas.

    this.jogadores = {
        1: {nome: 'Ono'},
        2: {nome: 'Ana'},
        3: {nome: 'Ini'},
        4: {nome: 'Uno'},
    };

    // Aqui é onde iremos acumular os contadores de votos:

    this.selecionados = {};
})
.controller('votoController', function($scope, jogadorService){

    // Vamos tornar o serviço acessível pela view...

    $scope.svc = jogadorService;
    
    // E implementar o mecanismo de votação:

    $scope.votar = function(k){
    
        if (!jogadorService.selecionados[k]) 
          jogadorService.selecionados[k] = 0;
    
        jogadorService.selecionados[k]++;
    }

})
.controller('visualizacaoController', function($scope, jogadorService){

    // Previsamos disponibilizar o serviço para a view do segundo
    // controller também:

    $scope.svc = jogadorService;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.js"></script><divng-app="myApp">

Vote aqui:

  <table ng-controller='votoController'>
    <tr ng-repeat='(k,v) in svc.jogadores'>
    <td>{{k}}</td>
    <td>{{v.nome}}</td>
    <td><button ng-click='votar(k)'>Votar</button></td>
    </tr>
  </table>
  
  <br/>
  
  Votados:
  
  <table ng-controller='votoController'>
    <tr ng-repeat='(k, v) in svc.selecionados'>
    <td>{{k}}</td>
    <td>{{svc.jogadores[k].nome}}</td>
    <td>{{v}}</td>
    </tr>
  </table>
  
  
</div>
    
04.06.2017 / 00:25
0

Add a button inside your ng-repeat that calls a controller "Edit" and pass your model. Create the method in the controller that receives the model and pass this value to a second model and call another view.

$scope.editUser = function(user) {
    $scope.user = angular.copy(user);
}

$scope.updateUser = function(user) {
    $http.post('api/updateUser.php', {
        "id": user.id,
        "fullname":user.fullname,
        "email":user.email
    }).success(function(data)
        alert("Update success");
    }).error(function(data, status){
        console.log("Error update user: "+data');
    });
}

The example above series a form .. Not the best way, but I hope you have understood. Look at this example on Github: Angular Series - Rodrigo Branas

    
03.06.2017 / 21:48