Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add animation when expression value changes - Angular 5

I have an expression on a page called 'pointsBalance' that displays numeric value. Its hooked up to an observable, and when pointsBalance value goes up, I want to change the colour to green then back to its original colour, and red if it goes down in value. I thought I could use Angular 5 new animation aliases :increment and :decrement but I've been having problems with it.

HTML to display the points balance:

<div [@valueAnimation]="pointsBalance">{{ pointsBalance }}</div> 

Code that sets the animation and the pointsBalance observable:

import { Component, OnInit, Input } from '@angular/core';
import { trigger, style, transition, animate, keyframes, query, 
    stagger, state, group } from '@angular/animations';
import { CompetitionsService } from "../../../shared/index";
@Component({
  selector: 'points',
  templateUrl: './...html',

  animations: [
    trigger('valueAnimation', [
      transition(':increment', group([
        query(':enter', [
          style({ color: 'green', fontSize: '50px' }),
          animate('0.8s ease-out', style('*'))
        ])
      ])),
      transition(':decrement', group([
        query(':enter', [
          style({ color: 'red', fontSize: '50px' }),
          animate('0.8s ease-out', style('*'))
        ])
      ]))
    ])
  ]
})
export class CompetitionDetailsComponent implements OnInit {

  pointsBalance: number;

  constructor(private competitionsService: CompetitionsService, ) { }

  ngOnInit() {

    this.competitionsService.updatePointsBalance$.subscribe(
      (pointsBalance) => {
        this.pointsBalance = pointsBalance;
      }
    )
  }
}

And when pointsBalance value changes I get this console error:

ERROR Error: Unable to process animations due to the following failed trigger transitions @valueAnimation has failed due to:

  • query(":enter") returned zero elements. (Use query(":enter", { optional: true }) if you wish to allow this.)

Does anyone know why I'm getting this error? Or is there another way to achieve this? Thanks.

like image 270
Jose the hose Avatar asked Apr 08 '18 14:04

Jose the hose


People also ask

What is the use of animateChild () function in Angular?

Each time an animation is triggered in Angular, the parent animation always gets priority and child animations are blocked. For a child animation to run, the parent animation must query each of the elements containing child animations and then let the animations run using the animateChild() function.

What is @keyframes in Angular?

keyframeslinkDefines a set of animation styles, associating each style with an optional offset value.

How do I use BrowserAnimationsModule?

The BrowserAnimationsModule must be imported in the main module of the application app. module. ts and after importing, it is necessary to restart the application so that the imported module is recognized.


1 Answers

the :enter animation will only animate elements that are newly created. Your "hacky" workaround works because it forces the inner span to get recreated whenever the value changes. I think what you want is to remove the :enter query. You want to animate when the number is in/decremented, and your animation has nothing to do with elements being added.

like image 174
Benjamin Kindle Avatar answered Oct 13 '22 11:10

Benjamin Kindle