0

When it comes to Ajax, it looks like Symfony (v. 2.7) and AngularJS (v. 1.4) don't go well together ;-)

I see two ways to make them cooperate - my question is: Which one is better?

  1. Adapt AngularJS to be compatible with Symfony
  2. Adapt Symfony to be compatible with AngularJS

For 1, this is the best instruction I found: http://harmssite.com/2014/09/mixing-angular-and-symfony/

Advantage: Can use $form->handleRequest($request); in Symfony to get all form fields at once.

Disadvantage: Cannot set default value's for <input>'s, cause AngularJS overwrites them. Solution:

  1. Set it in Angular's controller (i.e. in app.js). But if the value comes from the database (i.e. Symfony), how can you get it into app.js? Render the entire app.js file through Symfony's templating system? => Looks like overkill to me.
  2. Set it inside the HTML/twig file with ng-init, as shown here: AngularJS - Value attribute on an input text box is ignored when there is a ng-model used? => This is more a workaround than a solution ;-)

For 2, the major disadvantage is that you need to read each form field separately into Symfony, using $post = $this->getRequest()->getContent(); as shown here: AngularsJS POST JSON data to Symfony2
I haven't tried this approach, so I don't know if there are further problems down the way ;-)

Community
  • 1
  • 1
Thomas Landauer
  • 7,857
  • 10
  • 47
  • 99
  • I once fiddled around with the combo you're trying to pull off, I used FOSRestBundle, my code's still there if it can help you in any way whatsoever: https://github.com/Lauriy/plant-genetic-resources Sorry for not being able to give an answer here. It's hard to come up with a definite one. – Lauri Elias Aug 09 '15 at 19:51

2 Answers2

0

After fiddling around with this for a while, I finally came to a solution: Kick out AngularJS completely!

As far as I understand it now, AngularJS is only the way to go, if you are processing your forms completely (or at least mainly) on the client-side. I guess, that's what they mean by "Single Page Application (SPA)".

If you're using Symfony for form processing, AngularJS probably makes your life harder, not easier.

I'm now using plain jQuery for JavaScript, and everything works like a charm (including Ajax), much more hassle-free than AngularJS was.

Thomas Landauer
  • 7,857
  • 10
  • 47
  • 99
0

If you just need to handle AJAX Requests from AngularJS to Symfony2 server, using classic forms (without Symfony2 form builder), you can use this bundle :

qandidate/symfony-json-request-transformer

Add it as a dependency in your composer.json and handle your requests (POST & other methods) in the same way as classic request.

You can See my post about this problem : Handling HTTP Requests from AngularJS to Symfony2

Other tip, to use AngularJS in your twig templates, add this in your angular app :

var app = angular.module('yourApp', [],
function($interpolateProvider){
    $interpolateProvider.startSymbol('<%');
    $interpolateProvider.endSymbol('%>');
});

This will change your start symbol from {{ to <% and your end symbol from }} to %>

In my case, use AngularJS make my work easier and me more productive, especially coupled to a Symfony back-end, but it's very personal, and depending on why/how angular is used.

chalasr
  • 12,971
  • 4
  • 40
  • 82