View information in angular HTML

Asked

Viewed 417 times

1

Hello, person, I’m beginner in angular, javascript and etc. I’m trying to display the information of a json returned by a php script, but I’m having this error:

Duplicates in a Repeater are not allowed

Angular:

angular.module("moduloJogos", []);
angular.module("moduloJogos").controller("moduloJogosCTRL", function($scope, $http) {
  $http.get('jogos.php').then(function(response) {
    $scope.partidas = response.data;
    console.log(response.data);
  });
});

HTML. Table that displays:

<tbody ng-controller="moduloJogosCTRL">

  <tr ng-repeat="partida in partidas">
    <td class="timeCasa">{{partida.timeCasa}}</td>
    <td class="tdAposta"><input type="button" class="btn" id="{{casa + partida.idPartida + partida.cotTimeC}}" value="{{partida.cotTimeC}}"/></td>
    <td class="tdAposta"><input type="button" class="btn" id="{{empate + partida.idPartida + partida.cotEmp}}" value="{{partida.cotEmp}}"/></td>
    <td class="tdAposta"><input type="button" class="btn" id="{{fora + partida.idPartida + partida.cotTimeF}}" value="{{partida.cotTimeF}}"/></td>
    <td>{{partida.timeFora}}</td>
  </tr>

</tbody>

PHP that takes the information and mounts the json:

$partidasTotais = "[";

while($jogos = $select->fetch(PDO::FETCH_ASSOC)) {

  $timeCasa = $jogos['time_casa'];
  $timeFora = $jogos['time_fora'];
  $idMatch = $jogos['id_jogo'];


  $content = $chamaURL->retornaConteudo('https://api.soccerama.pro/v1.2/odds/match/'.$idMatch.'?api_token='.$apiTOKEN);
  $cotCasa = $content["data"]["0"]["types"]["data"]["0"]["odds"]["data"]["0"]["value"];
  $cotEmp = $content["data"]["0"]["types"]["data"]["0"]["odds"]["data"]["2"]["value"];
  $cotFora = $content["data"]["0"]["types"]["data"]["0"]["odds"]["data"]["1"]["value"];

  $partidaIndividual = '{"timeCasa":'. $timeCasa.',"timeFora":'. $timeFora.',"idPartida":'. $idMatch.',"cotTimeC":'. $cotCasa.',"cotEmp":'. $cotEmp.',"cotTimeF":'. $cotFora.'},';

$partidasTotais .= $partidaIndividual;


}

$partidasTotais .= "]";

echo json_encode($partidasTotais);

1 answer

1


This usually happens when you don’t have a unique id for ng-repeat, you can, according to documentationthe, use track by $index that will make the position inside the array become the ID and I recommend you also check if it has no elements Undefined inside your $scope.partidas.

Make some changes:

  • Puts $i = 0; before the while;
  • Change your assignment code as follows:

    if($i == 0) $partidaIndividual = '{"timeCasa":"'. $timeCasa.'","timeFora":"'. $timeFora.'","idPartida":"'. $idMatch.'","cotTimeC":"'. $cotCasa.'","cotEmp":"'. $cotEmp.'","cotTimeF":"'. $cotFora.'"}';
    else $partidaIndividual = ', {"timeCasa":"'. $timeCasa.'","timeFora":"'. $timeFora.'","idPartida":"'. $idMatch.'","cotTimeC":"'. $cotCasa.'","cotEmp":"'. $cotEmp.'","cotTimeF":"'. $cotFora.'"}'; 
    
  • At the end of the iteration puts the $i++;

  • Brackets are required not to remove them.
  • Can you explain to me what the $index would be? Because I tried to put exactly like this "start in matches track by $index" and it showed me many lines (beyond what the php script returns) and all empty. Already the $cope.matches is just right...

  • When you do console.log($scope.partidas); he returns a Array of Objects? Look at this example: ng-repeat="value in [4,4]" it would cause the same error that you have because it doesn’t have a single Key, using the track by $index it would use the position inside the Array (0 and 1) as Key.

  • He returns something like this: "[{\"timeCasa\":Hamilton Academical,\"timeFora\":Kilmarnock,\"idPartida\":688005,\"cotTimeC\":2.15,\"cotEmp\":3.30,\"cotTimeF\":3.35},{\"timeCasa\":Aberdeen,\"timeFora\":Partick Thistle,\"idPartida\":688009,\"cotTimeC\":1.53,\"cotEmp\":4.05,\"cotTimeF\":6.05},]"

  • This Json of yours is very invalid, try to remove the json_encode and see what he returns.

  • I took json_encode and it failed Unexpected token H in JSON. I removed the square bracket of the beginning and end of the json and it returned right, but with the track by $index, on the screen appears a huge number of empty lines again.

  • Send me how JSON is now

  • {"timeCasa": Hamilton Academical,"timeFora": Kilmarnock,"idPartida": 688005,"cotTimeC": 2.15,"cotEmp": 3.30,"cotTimeF": 3.35},{"timeCasa": Aberdeen,"timeFora": Partick Thistle,"idPartida": 688009,"cotTimeC": 1.53,"cotEmp": 4.05,"cotTimeF": 6.05},{"timeCasa": Rangers,"timeFora": Ross County,"idPartida": 689806,"cotTimeC": 1.44,"cotEmp": 4.60,"cotTimeF": 6.55},{"timeCasa": Inverness CT,"timeFora": Dundee,"idPartida": 689809,"cotTimeC": 2.05,"cotEmp": 3.30,"cotTimeF": 3.65},{"timeCasa": Motherwell,"timeFora": Hearts,"idPartida": 689814,"cotTimeC": 3.80,"cotEmp": 3.70,"cotTimeF": 1.87},

  • your JSON needs to look like this: [ 0 => {"timeCasa": Hamilton Academical,"timeFora": Kilmarnock,"idPartite": 688005,"cotTimeC": 2.15,"cotEmp": 3.30,"cotTimeF": 3.35}, 1 => {... }];

  • Aeee, it worked! Thank you!

  • You’re welcome @Krint we’re here to strengthen the community, so I’m happy to help :)

Show 5 more comments

Browser other questions tagged

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