0

I am writing an angular 2 app. In the quick start tutorial, all things are working fine but in my todo app, its not working.

app.component.ts

import {Component, Template, View, NgFor, bootstrap, CORE_DIRECTIVES, FORM_DIRECTIVES} from 'angular2/angular2';

@Component({
    selector: 'todo-app',
})

@View({
  directives : [NgFor, CORE_DIRECTIVES, FORM_DIRECTIVES]
})

@Template({
    url: 'components/todo-app/todo-app.html'
 })

class TodoAppComponent {
  todos : Array;
  constructor() {
    this.todos = [];

    TodoFactory.getAll().then((data) =>{
       this.todos = data; // I got JSON in todos object.
    });
  }
}

todo-app-html

<div *ng-for='#todo of todos'>
    {{ todo.text }}
</div>

In my template, *ng-for is not working. Even when I try {{ todos }}, it shows all of the data. I searched a lot and found that I should pass NgFor core directives to the template like directives : [NgFor] This is also not working. Any single help would be appreciated.

Vineet
  • 4,525
  • 3
  • 23
  • 42

4 Answers4

2

You should *ngFor instead of *ng-for:

<div *ngFor="#todo of todos">
  {{ todo.text }}
</div>

Otherwise, you don't need anyore to specify the NgFor, core and form directives...

Edit

Perhaps you load your data outside the context of Angular2. In this case you could try to leverage the NgZone class, as described below:

class TodoAppComponent {
  todos : Array;
  constructor(private ngZone:NgZone) {
    this.todos = [];

    TodoFactory.getAll().then((data) =>{
      this.ngZone.run(() => {
        this.todos = data; // I got JSON in todos object.
      });
    });
  }
}

See this plunkr for more details: https://plnkr.co/edit/KKj1br?p=preview

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
0

Which version of Agular2 do you use????

For latest version you may try this,

import {Component, Template, View, bootstrap, FORM_DIRECTIVES} from 'angular2/angular2';

change them to,

import {Component,View,bind} from 'angular2/core';
import {bootstrap}  from 'angular2/platform/browser';
import {FORM_DIRECTIVES,CORE_DIRECTIVES,FormBuilder, Validators } from 'angular2/common'; // CORE_DIRECTIVES contains all common required API 

then,

<div *ngFor="#todo of todos">
  {{ todo.text }}
</div>
micronyks
  • 54,797
  • 15
  • 112
  • 146
0

The angular team removed every dashes (-) from template directives in the beta releases.

Therefore:

ng-for, ng-model, ng-class,...

are now:

ngFor, ngModel, ngClass,...

when using angular2.0.0-beta.0 and more

Pat M
  • 5,966
  • 7
  • 24
  • 34
0

In RC version of Angular 2 , angular team has removed # from templating syntax inside *ngFor and has changed *ng-for to *ngFor , Use let instead of #

Previously you were using

<div *ng-for='#todo of todos'>
    {{ todo.text }}
</div>

You should use

<div *ngFor='let todo of todos'>
    {{ todo.text }}
</div>
Rajesh Patel
  • 1,946
  • 16
  • 20