Pass php array to iterate in view with angularJS

0

I have this php array that has different years in it:

array(6) {
  [0]=>
  int(2017)
  [1]=>
  int(2016)
  [2]=>
  int(2015)
  [3]=>
  int(2014)
  [4]=>
  int(2013)
  [5]=>
  int(2012)
}

And I want to pass it to a structure that I can use in angularjs, like this:

$scope.miarray = response.data;

To then be able to iterate with ng-repeat in a view and generate a selector with classic html options, with the years to choose from.

Take the array and re-encode it with json_encode() , get:

string(97) "[{"scalar":2017},{"scalar":2016},{"scalar":2015},{"scalar":2014},{"scalar":2013},{"scalar":2012}]"   pero en la vista, por consola, obtengo "message": "Undefined offset: 0",

I will appreciate help in this regard, Thank you!

    
asked by rendor9 13.09.2017 в 03:49
source

2 answers

0

Something like that should work:

<php
$data = array(2017, 2016, 2015, 2014, 2013, 2012);
// respuesta ajax
header('application/json');
print json_encode(array('years' => $data));

Then in AngularJS, you should receive the data in response.data.years , which should result in something like:

console.log(response.data.years);

Object { years: Array[6] }

0: 2017,
1: 2016,
2: 2015,
3: 2014,
4: 2014,
5: 2012
    
answered by 13.09.2017 / 06:41
source
0

Finally, I was able to send the data from php to angular (as indicated by @CJ Nimes), using.

$ misanios = json_encode (array ('elanio' = > $ misanios));

what generates me: string (42) "{" elanio ": [2017,2016,2015,2014,2013,2012]}"

that I could receive within angular with

$ scope.response = response.data.elanio;

    
answered by 13.09.2017 в 15:38