Insert array within a scope of the angle

2

Hello, guys, I'm a beginner in angular, and I'd like to know what I'm doing wrong here. Look at this: Every time I click a button I want it to add some "modified" information from an object called a match to the betting object. The problem is that in fact, instead of adding it it overrides.

This is the function:

  $scope.fazAposta = function(partida, tipo) {
    var time;
    var partidaA = partida.timeCasa + " x " + partida.timeFora;
    var cotacaoA;
    var idpart = partida.idPartida;
    switch (tipo) {
      case 1:
        cotacaoA = partida.cotTimeC;
        time = partida.timeCasa;
        break;
      case 2:
        cotacaoA = partida.cotTimeF;
        time = partida.timeFora;
        break;
      case 0:
        cotacaoA = partida.cotEmp;
        time = "Empate";
        break;
    }

    $scope.apostas = [
      {timeApostado: time, partidaApostada: partidaA, cotacaoApostada: cotacaoA, idDaPartida: idpart}
    ];

    $scope.apostas.push(angular.copy(apostas));

  }

Oh, if it is necessary in any way, the starting object is coming from ajax made by one of a php script.

Here's where I'm displaying some betting information:

  <h3 id="apH3">Apostas Simples</h3>
    <div class='apostado' ng-repeat="aposta in apostas track by $index">
      <h1 name='time'>{{aposta.timeApostado}}</h1>
      <h2 name='partida'>{{aposta.partidaApostada}}</h2>
      <h2 name='cotacaoApostada'>{{aposta.cotacaoApostada}}</h2>
    </div>
  </div>
    
asked by anonymous 09.02.2017 / 14:22

2 answers

2

On line:

$scope.apostas = [
  { timeApostado: time, 
    partidaApostada: partidaA, 
    cotacaoApostada: cotacaoA, 
    idDaPartida: idpart }
];

You are setting the value of the apostas property of the current $scope to an array containing a member.

however on the next line you are pushing, adding another object.

Your always result will be an array with two objects.

If you want to preserve the betting array, move the array initialization out of the method:

.controller('nomeControle', function($scope) {
    $scope.apostas = []; 
    [...]

And, in the method, perform only push() :

var aposta = { 
    timeApostado: time, 
    partidaApostada: partidaA, 
    cotacaoApostada: cotacaoA, 
    idDaPartida: idpart };

$scope.apostas.push(aposta);
    
09.02.2017 / 15:09
2

Hello you are really about writing the Array. Remove this part

$scope.apostas = [
      {timeApostado: time, partidaApostada: partidaA, cotacaoApostada: cotacaoA, idDaPartida: idpart}
    ];

    $scope.apostas.push(angular.copy(apostas));

And add PUSH this way:

$scope.apostas.push({
        timeApostado: time, 
        partidaApostada: partidaA,
        cotacaoApostada: cotacaoA, 
        idDaPartida: idpart
    });
    
09.02.2017 / 15:00