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);
						
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...
– Krint
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.– Thiago Santos
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},]"– Krint
This Json of yours is very invalid, try to remove the
json_encodeand see what he returns.– Thiago Santos
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.– Krint
Send me how JSON is now
– Thiago Santos
{"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},– Krint
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 => {... }];
– Thiago Santos
Aeee, it worked! Thank you!
– Krint
You’re welcome @Krint we’re here to strengthen the community, so I’m happy to help :)
– Thiago Santos