Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a transition when changing properties in angular 2/4

I want to create a transition effect whenever I change the value of a property.

I tried doing the following

    @Component({   
    selector: 'image-holder',
    template: `
        <div class="carousel-image">
            <img src="{{ image }}" [@slideInRight]="slide" />
            <span>{{ text }}</span>
        </div>
    `,
    styleUrls: ['../greenscreen.scss'],
    animations: [
        trigger(
            'slideInRight',
            [
                transition(
                ':enter', [
                style({transform: 'translateX(100%)', opacity: 0}),
                animate('500ms', style({transform: 'translateX(0)', opacity: 1}))
                ]
            ),
            transition(
                ':leave', [
                style({transform: 'translateX(0)', 'opacity': 1}),
                animate('500ms', style({transform: 'translateX(100%)',opacity: 0}))
                ]
            )
            ])
    ]
})
export class ImageHolderComponent implements OnChanges {
    @Input()
        image: string;
    @Input()
        text: string;

    public slide: boolean = true;

    public ngOnChanges(changes: { [propKey: string]: SimpleChange }){
        this.slide = !this.slide;
    }
}

What I assumed was changing the property would trigger the component to start the animation effect again but that doesn't work as expected

like image 771
Bazinga777 Avatar asked May 14 '17 18:05

Bazinga777


People also ask

How do you define transition between two states in Angular?

In Angular, transition states can be defined explicitly through the state() function, or using the predefined * (wildcard) and void states.

What is transition function in Angular?

The State Change Expression instructs Angular when to run the transition's animations, it can either be. a string with a specific syntax. or a function that compares the previous and current state (value of the expression bound to the element's trigger) and returns true if the transition should occur or false otherwise.

How do you define transition animation between two states inactive to active )?

Transitions between two states take place so that we can build simple animations between two states driven by a model attribute. Transition basically means navigating from the current state to a new state. In angular, the transition is an animation-specific function which is used in angular's animation DSL language.


1 Answers

You can use *ngFor for those use cases, even if it's just a single object.

@Component({
  selector: 'image-holder',
  template: `
    <div class="carousel-image">
      <img [src]="image" *ngFor="let image of [image]" [@slideInRight]/>
      <span>{{ text }}</span>
    </div>
  `,
  styleUrls: ['../greenscreen.scss'],
  animations: [
    trigger(
      'slideInRight',
      [
        transition(
          ':enter', [
            style({transform: 'translateX(100%)', opacity: 0}),
            animate('500ms', style({transform: 'translateX(0)', opacity: 1}))
          ]
        ),
        transition(
          ':leave', [
            style({transform: 'translateX(0)', 'opacity': 1}),
            animate('500ms', style({transform: 'translateX(-100%)', opacity: 0}))
          ]
        )
      ])
  ]
})
export class ImageHolderComponent {
  @Input()
  image: string;
  @Input()
  text: string;

}
like image 163
Martin Cremer Avatar answered Oct 08 '22 22:10

Martin Cremer