Angularjs 如何在Symfony 2.3中使用Angular.js在Twig中显示db结果数组?
我正在尝试将angular.js包含在使用Symfony 2.3创建的项目中。 我使用controller中的json_encode函数将结果数组转换为jsonAngularjs 如何在Symfony 2.3中使用Angular.js在Twig中显示db结果数组?,angularjs,symfony-2.3,Angularjs,Symfony 2.3,我正在尝试将angular.js包含在使用Symfony 2.3创建的项目中。 我使用controller中的json_encode函数将结果数组转换为json function displayAction(){ $user= $this->getDoctrine()->getRepository('DemoBundle:Users'); $result = $user->findAll(); $res= json_encode($result);
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>
[[res.id]]
[[res.username]]
[[res.password]]
但是它给了我一个空的json数组。我想返回json在Twig模板中处理它是没有意义的。也许您需要在angularjs控制器上请求json资源(使用$http或通过$resource),并从模型中获取数据(无ng init)
如果你还想这样做,也许你可以从这里得到一些帮助:如果你使用的是角度绑定表达式是
{}
不是[[]]
我知道这一点,但是twig也使用{{}}来显示值,所以我用这段代码将角度大括号{{}改为[[]]。var myApp=angular.module('myApp',[],函数('interpolateProvider){$interpolateProvider.startSymbol('[');$interpolateProvider.endSymbol(']');});