0

I created an array of cards in php:

$card = array(
      "id" => $row["id"],
      "nome" => $row["nome"],
      "indirizzo" => $row["indirizzo"],
      "descrizione" => $row["descrizione"],
      "prezzo" => $row["prezzo"],
      "images" => array(),
      "thumbs" => array(),
      "dataInserimento" => $row["inserimento"],
      "dataModifica" => $row["modifica"],
      "bigImage" => array(),
      "lat" => $row["lat"],
      "lng" => $row["lng"],
      "latInd"=>$row["latInd"],
      "lngInd"=>$row["lngInd"]);

in a loop i do:

 $cards["card"][] = $card;

and i pass the data to angular:

echo json_encode($cards);

In controller i copy the array into a scope variable:

  $scope.cards=[];
  CardService.getCards($scope.lastCard).then(function (success) {
    CardService.setLocalCards(success.data.card);
    /*check if all imges are loaded*/
    for (var i = 0; i < success.data.card.length; i++) {
      console.log(success.data.card);
      console.log("numero di card caricate " + success.data.card[i].id);
      var localCard = CardService.getLocalCard(success.data.card[i].id);
      if (localCard === null || localCard.photosLoaded === true) {
        $scope.cards.push((success.data.card[i]));
        $scope.lastCard = success.data.card[i].id;
      }
    }
    $ionicLoading.hide();
    console.log("ultima scheda caricata " + $scope.lastCard);
  }, function (fail) {
    console.log("getCards error " + JSON.stringify(fail));
  });
  console.log($scope.cards);

the problem is if i try to access elements in $scope.cards i can access them from the then function in CardService but outside i get this:

enter image description here

enter image description here And i don't know how to access the elements

1 Answer 1

1

Since you are using promises, which are async, your console.log is executing right after the CardService.getCards function and before the function inside the .then(). The function inside the .then is a promise that excecutes only after the getCards is finished. So when you do console.log($scope.cards);The arrive it's still empty.

Either way be sure you are actually excecuting any push inside $scope.cards and you can try to do a console.log($scope.cards) inside the promise (after the cards are pushed) to see if it's actually filling.

Sign up to request clarification or add additional context in comments.

2 Comments

i answered my self, thanks to your answer. The problem was that first i fill the cards array then i change the page, and then when i go back i print cards and was empty because cards wasn't a session variable.But it is strange because when i print cards i se an empty array and when i open it i get the values from console but can't access them from code.
Great! Probably that last part is because it's filled when you print on the console but after that it's erased, maybe it's not saving properly on the variable to keep it on the $scope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.