Json_encode doesn't work in my Symfony2 controller
I am new to Symfony2 and AngularJS. I am trying to use json_encode to display the contents of my database. But it doesn't work. This is my 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)));}
This is my 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 %}
And my app.js:
angular.module('myApp1', []).
filter('sortData', function() {
alert('Hi');
return out;
});
When I refresh my page this is shown: T1 T2 {{data.nombreJuego}} {{data.description}}
What's wrong?
+3
source to share
1 answer
I don't like passing data from the back-end to. Like this, ajax request is usually used to fetch the data.
Try to pass the data to a javascript variable that you can influence is an object in your angular scope:
<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 %}
0
source to share