Angular 2 component *ngFor template parse error

Dennis picture Dennis · May 26, 2016 · Viewed 10.4k times · Source

I have the following error from angular 2:

EXCEPTION: Template parse errors:
Parser Error: Unexpected token . at column 26 in [ngFor let button of modal.buttons] in ModalComponent@11:22 ("">
                        <button type="button" class="btn" data-dismiss="modal"
                          [ERROR ->]*ngFor="let button of modal.buttons"
                          [ngClass]="button.classes"
                   "): ModalComponent@11:22

This is my code:

import {Component} from 'angular2/core';
import {NgFor, NgClass} from 'angular2/common';

import {EventService} from '../services/event.service';

interface Button {
  text: string;
  classes: Array<string>;
  clicked: Function;
}

interface Modal {
  title: string;
  text: string;
  buttons: Array<Button>;
}

@Component({
  selector: 'modal',
  template:  `<div *ngIf="modal" class="modal-dialog" role="document">
                <div class="modal-content">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title">{{modal.title}}</h4>
                  </div>
                  <div class="modal-body" [innerHTML]="modal.text"></div>
                  <div class="modal-footer">
                    <button type="button" class="btn" data-dismiss="modal"
                      *ngFor="let button of modal.buttons"
                      [ngClass]="button.classes"
                    >{{button.text}}</button>
                  </div>
                </div>
              </div>`,
  directives: [NgFor, NgClass]
})
export class ModalComponent {
  modalElement: HTMLElement;
  modal: Modal = [...];

  [...]

}

I try to generate a modal component, without the buttons it works fine. Why is this wrong? Why cant angular iterate over an object child array? I am searching for hours, please help

Answer

David Gabrichidze picture David Gabrichidze · May 26, 2016

In angular2.rc1 syntax of *ngFor has changed, from *ngFor="#item in items" to *ngFor="let item in items";

You are using new syntax, but have old version of angular2. I supposed it based on your imports (you are referencing to 'angular2/...', instead of '@angular/...').

Try old syntax or update to new version of angular.