How to pass an Array to Angularjs?

Asked

Viewed 326 times

2

array (
  0 => 
  Pessoa::array (
      'nome' => 'Paulo',
      'idade' => 15,
    )


app.controller('meuCrtl', function ($scope, $http, $timeout) {
 $http.get('ajax/getPessoa.php').success(function(data){
 $scope.list = data;

...

I don’t want the data to be visible on getPessoa.php. Is there any other way to pass my data(array) to Angularjs so that it is hidden?

  • Yes, for your data to be hidden, Oce needs to encrypt it in PHP before passing it to the client. I found this framework here that can help you in this solution http://www.jcryption.org/

1 answer

1

You need to serialize the array as JSON, which is what Angular is waiting to receive:

<?php
$dados = array (
    array (
        'nome' => 'Paulo',
        'idade' => 15,
    ),
    array (
        'nome' => 'João',
        'idade' => 20,
    )
);

header('Content-Type: application/json');
echo json_encode($dados);

I don’t want the data to be visible in getPessoa.php.

I don’t know if I understood this part right, but if you want that if the person type the URL of that file in the browser does not appear anything, no, it is not possible. If you prevent the browser from seeing the data, Angular will also not be able to see them.

Browser other questions tagged

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