Using Typescript & Angular 2.0.0-rc.4
How can I specify style property values from the template so that I can re-use buttons? For example, if I wanted to specify a different background-color for each button based on some property that is bound by the template. See below
Assume the following component:
import {
Component,
OnInit,
OnDestroy,
Input,
style,
state,
animate,
transition,
trigger
} from '@angular/core';
@Component({
selector: 'my-toggle-button',
template: `<div @state="state" (click)="click()">{{bgColor}}</div>`,
animations: [
trigger('state', [
state('inactive', style({
'color': '#606060'
})),
state('active', style({
'color': '#fff',
'background-color': '#606060' // I want this to be bgColor
})),
transition('inactive <=> active', animate('100ms ease-out'))
])
]
})
export class ToggleButtonComponent implements OnInit {
@Input() bgColor: string;
state: string = 'inactive';
active: boolean = false;
ngOnInit() {
this.state = this.active ? 'active' : 'inactive';
}
click() {
this.active = !this.active;
this.state = this.active ? 'active' : 'inactive';
}
}
calling template:
<h1>Animated Directive</h1>
<my-toggle-button [bgColor]="'#f00'"></my-toggle-button>
<my-toggle-button [bgColor]="'#0f0'"></my-toggle-button>
<my-toggle-button [bgColor]="'#00f'"></my-toggle-button>