Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Template parse errors when using ngFor inside div

Why Angular 2 doesn't let me create set of child views inside div-block using *ngFor?

The case:

import {Component, Input} from '@angular/core';
import {ChoiceComponent} from './choice.component';
import {Question} from './model/Question';

@Component({
    selector: 'question',
    template: `
    <div class="panel">
    {{question.text}}
    <choice *ngFor="let choice of question.choices" [choice]="choice">
    </div>
    `,
    directives: [ChoiceComponent]
})
export class QuestionComponent {

    @Input()
    question: Question; // Input binding from category component ngFor
}

causes

EXCEPTION: Template parse errors:
Unexpected closing tag "div" ("
    <div class="panel">
    <choice *ngFor="let choice of question.choices" [choice]="choice">
    [ERROR ->]</div>
    "): QuestionComponent@3:4

However following works fine, but I want to have question and choice buttons inside a same panel.

<div class="panel">
  {{question.text}}
</div>
<choice *ngFor="let choice of question.choices" [choice]="choice">
like image 940
Tuomas Toivonen Avatar asked May 29 '16 06:05

Tuomas Toivonen


2 Answers

Try this one :-

<div class="panel" *ngFor="let choice of question?.choices">
 {{question.text}}
 <choice [choice]="choice"> </choice>
</div>

By doing so you are repeating div block and for each repeat there is one text and button. if something unclear yet please ask me or post plunker if possible.

like image 43
Pardeep Jain Avatar answered Sep 27 '22 22:09

Pardeep Jain


Parser expects <choice> tag to be closed first until <div>.

Try following

<div class="panel">
  {{question.text}}
  <choice *ngFor="let choice of question.choices" [choice]="choice">
  </choice>
</div>
like image 88
tchelidze Avatar answered Sep 27 '22 21:09

tchelidze