Angular 2 by Examples: Difference between revisions

From Han Wiki
Jump to navigation Jump to search
add ngFor to iterate through an array
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
= ngFor to iterate through an array =
= Basics =
 
== Using variable ==
 
<syntaxhighlight lang="ts">
import { Component } from '@angular/core';
 
@Component({
        selector: 'my-app',
        template: `
<div>Hello {{ name }}</div>
`
})
 
export class AppComponent {
        name: string;
 
        constructor() {
                this.name = 'Michael';
        }
}
</syntaxhighlight>
 
<div class="mw-geshi">Hello Michael</div>
 
== ngFor to iterate through an array ==


<syntaxhighlight lang="ts">
<syntaxhighlight lang="ts">
Line 21: Line 46:
}
}
</syntaxhighlight>
</syntaxhighlight>
<div class="mw-geshi">
* Hello Ari
* Hello Carlos
* Hello Felipe
* Hello Nate
</div>

Latest revision as of 13:58, 8 July 2016

Basics

Using variable

import { Component } from '@angular/core';

@Component({
        selector: 'my-app',
        template: `
<div>Hello {{ name }}</div>
`
})

export class AppComponent {
        name: string;

        constructor() {
                this.name = 'Michael';
        }
}
Hello Michael

ngFor to iterate through an array

import { Component } from '@angular/core';

@Component({
        selector: 'my-app',
        template: `
<ul>
        <li *ngFor="let name of names">Hello {{ name }}</li>
</ul>
`
})

export class AppComponent {
        names: string[];

        constructor() {
                this.names = ['Ari','Carlos','Felipe','Nate'];
        }
}
  • Hello Ari
  • Hello Carlos
  • Hello Felipe
  • Hello Nate