Einius
Einius

Reputation: 1382

Using Twig variable in AngularsJS

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?

Upvotes: 2

Views: 1122

Answers (2)

LordTribual
LordTribual

Reputation: 4249

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.

Upvotes: 5

Med
Med

Reputation: 2063

Use temporary javascript variable to store the twig variable

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

Upvotes: 1

Related Questions