Insert array within an angular scope

Asked

Viewed 637 times

2

Hello, everyone, I’m a beginner in angular, and I’d like to know what I’m doing wrong here. Check it out: Every time I click on a button I want it to add some "modified" information of an object called a match on the bet object. The problem is that actually, instead of adding it to it.

That 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));

  }

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

Here’s where I’m displaying some bet 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>

2 answers

2

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

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

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

And adds PUSH that way:

$scope.apostas.push({
        timeApostado: time, 
        partidaApostada: partidaA,
        cotacaoApostada: cotacaoA, 
        idDaPartida: idpart
    });
  • Thank you so much! I kind of used your two answers.

  • A lot of nothing, we’re here to help =)

2


On the line:

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

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

but in the next line you are running a push, adding another object.

Its result at all times will be an array with two objects.

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

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

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

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

$scope.apostas.push(aposta);
  • Thank you, man! It really helped

  • @Krint not so, it’s always a pleasure. I’m glad it worked!

Browser other questions tagged

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