Angular2 анимация с переменными стилями
Использование Typescript и Angular 2.0.0-rc.4
Как я могу указать значения свойств стиля из шаблона, чтобы я мог повторно использовать кнопки? Например, если я хотел указать другой цвет фона для каждой кнопки на основе какого-либо свойства, связанного с шаблоном. Увидеть ниже
Предположим, следующий компонент:
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';
}
}
вызывающий шаблон:
<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>