4

I have component PageComponent which data is renewed on different url. I want animate when Page model is changed. In my given sample animation works only first time when PageComponent is loaded.

What should I add / change that animation would work when model is updated?

page.component.ts

@Component({
    selector: 'my-page',
    templateUrl: './page.component.html',
    styleUrls: ['./page.component.scss'],
    providers: [PageService],
    pipes: [SafePipe],
    host: { '[@routeAnimation]': 'true' },
    animations: Animations.page
})
export class PageComponent implements OnInit {

    page: Page;

    constructor(private route: ActivatedRoute,
                private pageService: PageService) {
    }

    ngOnInit() {
        this.route.params.forEach((params: Params) => {
            let uri = params['uri'];
            this.getPage(uri);
        });
    }

    getPage(uri: string) {
        this.pageService.getPage(uri).subscribe(
            page => this.page = page,
            error => this.errorMessage = <any>error
        );
    }
}

animation.ts

import {style, animate, transition, state, trigger} from '@angular/core';

export class Animations {
    static page = [
        trigger('routeAnimation', [
            transition('void => *', [
                style({
                    opacity: 0,
                    transform: 'translateX(-100%)'
                }),
                animate('2s ease-in')
            ]),
            transition('* => void', [
                animate('2s 10 ease-out', style({
                    opacity: 0,
                    transform: 'translateX(100%)'
                }))
            ])
        ])
    ];
}
Ugnius Malūkas
  • 2,649
  • 7
  • 29
  • 42

1 Answers1

0

I had the same issue and I solved it using steps described in the article Using ChangeDetection With Animation To Setup Dynamic Void Transitions In Angular 2 RC 6. There are basically 3 steps you need to do:

  1. Import ChangeDetectorRef class

    import { ChangeDetectorRef } from "@angular/core";
    
  2. Create instance of this class using dependency injection

    constructor(private changeDetector: ChangeDetectorRef) {}
    
  3. Call method detectChanges() on your instance of the ChangeDetectorRef class after the change

    // Change your model ...
    SomeMethodWhichChangesTheModel();
    
    // ... and call the detectChanges() method on the ChangeDetectorRef
    this.changeDetector.detectChanges();
    
Jan.T
  • 56
  • 3