nykon
nykon

Reputation: 625

django's template tags and angularjs

I have a small puzzle, I have in Django's template a following line of code:

{% if x == y %} sth {% endif %}

Where "x" variable belongs to Django while "y" is a angularjs variable. I've learned that one can change the $interpolateProvider to {$ $} or sth else, but in this case that's not an option since the code should than look like:

{% if x == {$ y $} %} sth {% endif %}

and that of course will cause an error. How to resolve that issue, i.e. to use angularjs variables inside the template tags?

I would be grateful for a tip,

best wishes,

nykon

Upvotes: 1

Views: 157

Answers (2)

AlvaroAV
AlvaroAV

Reputation: 10563

I'm not sure if this solution fits for you, but one option I've used some times is JavaScript, I passed Django & AngularJS to JavaScript and do the conditionals and actions there.

Something like:

<script>
var angular_variable = {$ y $}
var django_variable = {{x}}
// Depending on your example you may need to enclose the variables with ' '
var angular_variable = '{$ y $}'
var django_variable = '{{x}}'

if (anguar_variable == django_variable){
   // Do whatever you need to do
}

</script>

Upvotes: 0

samu
samu

Reputation: 3120

To be honest, I think the best solution is NOT to mix Django template language with angular. If you really need to depend on a django-provided variable, then you should pass it to the template, store it in a JS variable with something like:

var myVariable = {{ django_provided_var }};

Inside a <script> tag, and then use angular to do the display logic.

Upvotes: 1

Related Questions