0

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?

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Thank you for your answer. I was thinking about ajax in agularjs controller. I will continue with this solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.