Reputation: 1212
I'm new to Angular and Symfony and I'm asking if variables that I give to the view with twig can be used by an angular controller?
Here is an example :
PHP Controller:
return $this->render('view.html', array('variable' => $value));
Angular Controller:
var data = 'variablefromtwig';
Anyone knows how to do it?
Upvotes: 0
Views: 2328
Reputation: 3755
symfony controller:
return $this->render('view.html', array('twigvar' => $value));
twigtemplate:
<script>
var app = angular.module('app', []);
app.value('twigVar', '{{twigvar}}');
app.controller('myController', function(twigVar){
alert(twigVar);
});
</script>
https://docs.angularjs.org/api/auto/service/$provide#value
Upvotes: 4
Reputation: 26
You can print this variable in JS value assignment statement.
In your twig template, set JS variable. It should be visible in angular controllers:
<script>
var data = '{{variable}}';
</script>
But in my opinion best solution is to read all required data through async ajax request just in angular controllers.
Upvotes: 0