0

in cakephp I have some data encoded to json ( like this: $projects= json_encode($projects); ) but my php controller returnet wrong data mode! my json returned data is in "[ ]" and i cant access to this form of data in angular ng-repeat for view! what is the matter?

here is some parts of my cakephp controller:

   public function admin_getprojects()
   {

        $projects = $this->Project->find('all',array(
            'contain'=>array('UsersProjects'=>array('User'),
                            'Task')
        ));
        $this->set('projects', $projects);

        $projects= json_encode($projects);
        echo $projects;
        exit();

    }

returned data: view of json data in json parser... this shouldn't have "[" and "]" at begin and end

ham-sandwich
  • 3,975
  • 10
  • 34
  • 46

2 Answers2

0

and i cant access to this form of data in angular ng-repeat for view! what is the matter?

That's not true, read the documentation.

[] is not wrong but an array, a collection of objects. See this question. I don't see the problem, angular can work with that data. See the documentation of ng-repeat for an example:

<div ng-init="friends = [
  {name:'John', age:25, gender:'boy'},
  {name:'Jessie', age:30, gender:'girl'},
  {name:'Johanna', age:28, gender:'girl'},
  {name:'Joy', age:15, gender:'girl'},
  {name:'Mary', age:28, gender:'girl'},
  {name:'Peter', age:95, gender:'boy'},
  {name:'Sebastian', age:50, gender:'boy'},
  {name:'Erika', age:27, gender:'girl'},
  {name:'Patrick', age:40, gender:'boy'},
  {name:'Samantha', age:60, gender:'girl'}
]">

See the evil [] in the data above? It works just fine:

  I have {{friends.length}} friends. They are:
  <input type="search" ng-model="q" placeholder="filter friends..." aria-label="filter friends" />
  <ul class="example-animate-container">
    <li class="animate-repeat" ng-repeat="friend in friends | filter:q as results">
      [{{$index + 1}}] {{friend.name}} who is {{friend.age}} years old.
    </li>
    <li class="animate-repeat" ng-if="results.length == 0">
      <strong>No results found...</strong>
    </li>
  </ul>
</div>

This won't probably send the correct header and is not really well written code:

$projects= json_encode($projects);
echo $projects;
exit();

Read that chapter http://book.cakephp.org/3.0/en/views/json-and-xml-views.html for a better JSON implementation in CakePHP.

Community
  • 1
  • 1
floriank
  • 25,546
  • 9
  • 42
  • 66
0

That's not true, read the documentation. [] is not wrong but an array, a collection of objects. See this question. I don't >see the problem, angular can work with that data

oh yes! after reading ng-repeat documentation I changed my ng-binding mode and it's working now Thanks a lot :)