Skip to content

Instantly share code, notes, and snippets.

@jhades
Created May 12, 2016 07:48
Show Gist options
  • Save jhades/91bbd72f5315942bbdcdf71fc338f97d to your computer and use it in GitHub Desktop.
Save jhades/91bbd72f5315942bbdcdf71fc338f97d to your computer and use it in GitHub Desktop.
Angular 2 Core Directives NgStyle and NgClass
@Component({
selector: 'app',
directives: [Hero, Heroes],
template: `
<heroes>
<hero id="1" name="Superman"></hero>
<hero id="2" [name]="'Batman'"></hero>
<hero id="3" [name]="'Spiderman'" marvel="true"></hero>
<hero id="4" name="Batgirl"></hero>
<hero id="3" name="Hulk" marvel="true"></hero>
<hero id="5" name="Robin"></hero>
<hero id="6" name="Flash"></hero>
</heroes>
`
})
export class App {
}
bootstrap(App);
@Directive({
selector: 'hero',
})
export class Hero {
@Input()
id: number;
@Input()
name:string;
@Input()
marvel = false;
}
export const BLUE = '#b13138';
export const RED = '#1976d2';
@Component({
selector:'heroes',
template: `
<table>
<thead>
<th>Name</th>
<th>Index</th>
</thead>
<tbody>
<tr *ngFor="let hero of heroes; let i = index"
[ngClass]="classes(hero)">
<td>{{hero.name}}</td>
<td>{{i}}</td>
</tr>
</tbody>
</table>
`
})
export class Heroes {
@ContentChildren(Hero)
heroes: QueryList<Hero>;
get styles() {
return {
color: 'blue',
'text-decoration': 'underline'
};
}
classes(hero: Hero) {
return {
marvel:hero.marvel,
hulk: hero.name === 'Hulk'
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment