最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular 2 Template parse errors when using ngFor inside div - Stack Overflow

programmeradmin2浏览0评论

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 './choiceponent';
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 ponent 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">

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.ponent';
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 ponent 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">
Share Improve this question asked May 29, 2016 at 6:59 Tuomas ToivonenTuomas Toivonen 23.5k51 gold badges144 silver badges243 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

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>

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.

I would consider to Close with </choice>

发布评论

评论列表(0)

  1. 暂无评论