Using Twig variable in AngularsJS

1.1k views Asked by At

I'm using Symfony2 with Twig and AngularJS. I need to pass Twig variable to angular's function. Currently I tried doing that like this: ng-click="submitPost({{ channelName }})" but when I console.log to check that variable I see it as undefined. How do I pass Twig variables to angular functions?

2

There are 2 answers

2
Med On BEST ANSWER

Use temporary javascript variable to store the twig variable

var tmp = "{{ channelName }}";
ng-click="submitPost(tmp)"
0
LordTribual On

If you are using Twig with AngularJS you will most likly run into conflicts as you might know. One solution is to use Angulars $interpolateProvider to change the start and end interpolation tags like so:

angular.module('myApp', []).config(function($interpolateProvider){
    $interpolateProvider.startSymbol('{[{').endSymbol('}]}');
});

In this case Angular will use {[{ and }]} to interpolate expressions. It might be a bit odd to type but you can chose whatever you like. However, this will solve the conflicts between Twig and Angular.