0

I am trying to iterate through a django object rendered to an HTML page by the view. I defined an Angular 2 component in the following manner :-

@Component({

selector: 'search',
template:`

            <form [ngFormModel]="form" >
                  {% for category in categories_all %}
                  <li>
                    <div class="checkbox">
                      <label>
                        <input type="radio" name="category" 
                        id="category_{{category.id}}" 
                        value="{{category.name}}"
                        ngControl = "category"
                        #category="ngForm"
                        {% ifequal category.id|stringformat:"s"
                        param_values.category_id|stringformat:"s" %}
                         checked="checked"
                         {% endifequal %}
                         (click)="onClick()">
                 {{category.name}}
                      </label>
                    </div>
                  </li>{% endfor %}
            </form>
`,
providers:[ HTTP_PROVIDERS]


})

{% for category in categories_all %} is django templating syntax so angular2 is not identifying it. Is there any way to resolve this issue?

Subhajit
  • 361
  • 1
  • 4
  • 18

3 Answers3

0

Use the {% verbatim %} Django template tag and put your AngularJS template code between {% verbatim %} and {% endverbatim %}.

sahuk
  • 91
  • 1
  • 6
0

you should define your own template tags like this

AngularJS module (app.js)

angular.module('myapp', []).config(function($interpolateProvider) {
    $interpolateProvider.startSymbol('[[');
    $interpolateProvider.endSymbol(']]');
});

Webpage

<a>[[ variable ]]</a> 
0

You can get both django and angular2 template working without any conflicts.

If you are using Jinja2, then you can easily change the variable start string in Jinja2 options.

Refer my blog post for more details.

Aswin Kumar K P
  • 1,023
  • 2
  • 12
  • 21