As a normal scenario that many other developers encounter, we have mature Symfony2 / TWIG-based applications, and some of the html.twig templates are too overloaded with jQuery code and difficult to maintain. How about throwing jQuery and using Angular? Assuming I have some basic knowledge of Angular modules, controllers, services, and a deep understanding of Symfony2 / TWIG, the problem with my fist is , what is the best way to pass variables from my existing controller / twig patterns for Angular controllers?
I do not want to load the region through a separate JSON call and a separate controller in Symfony2. I just want to use the existing variables that I have on the branch.
One way to do this is to declare some global js variables:
<script> var window.someVar = {{ twig_object | json_encode() }}; </script>
a then do smth like
<div ng-controller="myCtrl" ng-init="init()"> <div ng-model="someVar"> .... </div> </div>
and in the controller
app.controller('myCtrl', ['$scope', function($scope) { $scope.init = function () { if (window['someVar']) { $scope['someVar'] = window['someVar']; } };
But it seems too ugly for me (3 steps) Can it be simplified at least or differently?
source share