1

我正在尝试在使用 Symfony 2.3 创建的项目中包含 angular.js。我使用控制器中的 json_encode 函数将结果数组转换为 json

function displayAction(){
    $user=  $this->getDoctrine()->getRepository('DemoBundle:Users');
    $result = $user->findAll();
    $res= json_encode($result);
    $res->header->set('Content-Type', 'application/json');
    return $this->render('DemoBundle:Demo:demoView.html.twig',array('results'=>$res));
}

在 ng-init 中初始化它,

<div ng-init= "result= {{results}}"></div>
<table>
  <tr ng-repeat="res in result">
      <td>[[res.id]]</td>
      <td>[[res.username]]</td>
      <td>[[res.password]]</td>
  </tr>
 </table>

但它给了我一个空的json数组。

4

1 回答 1

0

I guess it has no sense return a json to process it in the Twig template. Maybe you need to ask for the json resource on angularjs controller (with $http or via $resource) and get the data from the model (no ng-init).

If you still want to do it, maybe you get some help from here: Decoding JSON in Twig

于 2013-08-25T14:22:24.657 回答