Parameter in to Animation Angular2

2019-01-13 21:31发布

I'm trying to make a simple animation like the simple jQuery below

animate({'left' : left_indent})

I'm using the Angular2 Animations but the problem is how do I pass the left_indent parameter outside my Component Class in to the animation trigger?

animations: [
    trigger('flyInOut', [

        state('for', style({
            left: this.left_indent,
        })),

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

4条回答
啃猪蹄的小仙女
2楼-- · 2019-01-13 21:48

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>
查看更多
beautiful°
3楼-- · 2019-01-13 21:52

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.

查看更多
ら.Afraid
4楼-- · 2019-01-13 21:53

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>
查看更多
一夜七次
5楼-- · 2019-01-13 22:00

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.

查看更多
登录 后发表回答