A
A
Alex2014-10-18 04:05:27
symfony
Alex, 2014-10-18 04:05:27

How to properly work with symfony and angularjs ?

Goodnight!
I decided to see what kind of animal this angularjs is .
I work on symfony2 , I decided to try to implement the page on angularjs.
Interests as it is correct to work with the data in such sheaf.
The options that I see so far:
1) SymfonyController sends a selection from the database in the form of objects (json array, etc.) to the template (twig).
Next, we somehow have to give this array to angular. Actually interested in how?
ngInit? and into it something like ng-init="list='{{list|json_encode|raw}}'"
Angular controllers in a separate file.
2) Make $http ajax request to symfony controller which will return JsonResponse to us
Well, then give it to the template and let ngRepeat understand it there.
Maybe there are some other options?
PS
Angular changed brackets to square

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2014-10-18
Protko @Fesor

How to work properly with symfony and angularjs

Apart. Ideally, an Angular-based application is in one repository, Symfony-based in another. On the REST API server. You have to mix only in case of some optimizations. At first, I doubt that this is your case.
It's better to just wrap it in verbatim so that twig doesn't parse. In any case, there are a lot of options in which nothing needs to be screened at all. For example, if for some reason you need to always transfer some data, for example, translations or something else, you can do this:
<script>
angular.module('app.data', []).constant('myMandatoryData', {{ data | json }});
</script>

and already for the application to register the dependency on app.data.
In a word, try not to set templates for angular.js in twig at all. They can be compiled, collected in $tempalteCache (yes, if you have everything in the heap, you can safely throw away assetic and build on gulp / gassetic) and much more.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question