Parameter in to Animation Angular2

混江龙づ霸主 提交于 2019-12-17 07:23:14

问题


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 }),
            ]))
        ]),
    ])
]

回答1:


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>



回答2:


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>



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/41966673/parameter-in-to-animation-angular2

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