我是 Symfony2 和 AngularJS 的新手。我正在尝试使用 json_encode 来显示我的数据库内容。但它不起作用。这是我的 Controller :
public function catalogonewAction()
{
$data = $this->getDoctrine()->getManager()
->getRepository('AcmeRetroBundle:Game')->findAll();
return $this->render('AcmeRetroBundle:Default:catalogonew.html.twig',
array('data' => json_encode($data)));}
这是我的 html.twig:
{% verbatim %}
<div ng-app="myApp1" ng-init="mydata = {{ list|raw }}">
<table id="sortedData">
<tr><th>T1</th><th>T2</th></tr>
<tr ng-repeat="data in mydata | filter:sortData">
<td>{{data.nombreJuego}}</td>
<td>{{data.description}}</td>
</tr>
</table>
</div>
{% endverbatim %}
还有我的 app.js:
angular.module('myApp1', []).
filter('sortData', function() {
alert('Hi');
return out;
});
当我刷新我的页面时,它显示如下: T1 T2 {{data.nombreJuego}} {{data.description}}
怎么了?
最佳答案
我不喜欢将数据从后端传递到 anuglarJS之类的,使用ajax请求获取数据很常见。
尝试将数据传递给一个 javascript 变量,您可能会影响到 Angular 范围内的对象:
<script>
var list= {{ data }}
</script>
{% verbatim %}
<div ng-app="myApp1" ng-init="mydata = list">
<table id="sortedData">
<tr><th>T1</th><th>T2</th></tr>
<tr ng-repeat="data in mydata | filter:sortData">
<td>{{data.nombreJuego}}</td>
<td>{{data.description}}</td>
</tr>
</table>
</div>
{% endverbatim %}
关于php - json_encode 在我的 Symfony Controller 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30817647/