1

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

2 Answers 2

1

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.

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

Comments

0

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>

Comments

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.