Ghasem
Ghasem

Reputation: 15603

Assign Django variable value to AngularJS

I have this template tags in my html code

{{ djangoVar}}   //Django template tag
{$ angularVar $} //AngularJS template tag

How can I assign {{ djangoVar}} to {$ angularVar $} in my html?

Something like:

{% {$ angularVar $} = djangoVar %}

Upvotes: 2

Views: 1502

Answers (1)

Serafeim
Serafeim

Reputation: 15104

The angularVar must be assigned with javascript code. So, in your django template you can do something like

<script>
var django_variables = {};
django_variables.djangoVar = {{ djangoVar }};
</script>

So you declare a django_variables global variable. Then, in your angular controllers you can do:

function MyController($scope) {
    $scope.angularVar = django_variables.djangoVar;
}

The important thing is to run the 1st snippet inside a normal django html template that the context will be passed and {{ djangoVar }} will get its value.

Upvotes: 2

Related Questions