Parameter in to Animation Angular2

こ雲淡風輕ζ 提交于 2019-11-27 04:03:38

Now it's possible.

animations: [
    trigger('flyInOut', [

        state('for', style({
            left: '{{left_indent}}', // use interpolation
        }), {params: {left_indent: 0}}), // default parameters values required

        transition('* => for', [
            animate(2000, keyframes([
                style({ opacity: 1, transform: 'translateX(0)', offset: 0 }),
                style({ opacity: 1, transform: 'translateX(-300px)', offset: 0.5 }),
            ]))
        ]),
    ])
]

UPDATE (according to SplitterAlex answer):

in template (for Angular < 4.4.6):

<div [@flyInOut]="{value: triggerValue, left_indent: left_indent}"></div>

for Angular >= 4.4.6 template should be

<div [@flyInOut]="{value: triggerValue, params: {left_indent: left_indent}}"></div>
SplitterAlex

The accepted answer doesn't work for me with Angular 4.4.6

You have to wrap the param values in the template in an object params

Replace:

<div [@flyInOut]="{value: triggerValue, left_indent: left_indent}"></div>

With:

<div [@flyInOut]="{value: triggerValue, params: {left_indent: left_indent}}"></div>

i have one solution. But it is helpful only if you are trying to use same animation several times with different parameters that you already know.

For example, i have reusable animation for making slideUp-slideDown effect. And in collapsed state container must keep some height (that i already know for these containers).

Animation:

import { style, trigger, state, transition, animate } from "@angular/animations";

export const slideUpDownAnimation = (height) => {
    return trigger(`slideUpDownAnimation${height}`, [
        state('0', style({
            overflow: 'hidden',
            height: '*'
        })),
        state('1', style({
            overflow: 'hidden',
            height: `${height}px`
        })),
        transition('1 => 0', animate('400ms ease-in-out')),
        transition('0 => 1', animate('400ms ease-in-out'))
    ]);
};

In component's class:

import { slideUpDownAnimation } from "../../animations/slide-up-down.animation";

@Component({
    moduleId: module.id,
    selector: 'new-order',
    templateUrl: './new-order.component.html',
    animations: [
        slideUpDownAnimation(32), // will return trigger named "slideUpDownAnimation32"
        slideUpDownAnimation(60) // will return trigger named "slideUpDownAnimation60"
    ]
})
export class NewOrderComponent {...

And finaly, in component's html:

<div class="header-fields"
       [@slideUpDownAnimation32]="collapsedFields">
...

<div class="line-group"
           *ngFor="let group of lineGroups; let g = index"
           [@slideUpDownAnimation60]="group.collapsed">
...

Unfortunately it can not be used for dynamic parameters cuz you must define them in decorator & html.

Currently animations only allow for static definitions of values.

However, according to this git hub feature request raised in June 2016, there is a plan but it appears to still be on the backlog of features to add.

It hasn't been released yet.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!