Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular: Disable animation on page load

Angular 11. I have a list of inputs (wrapped by a div, each.) with the ability to add an input using reactive forms, so whenever the user clicks a button to add an input, I push a control to the inputs array.

The behavior I want is that whenever the user dynamically adds an input, it will show with an animation, so I wrote this code:

  animations: [
trigger('input', [
  state('in', style({ opacity: 1 })),
  transition(':enter', [
    animate(
      '.3s ease-in',
      keyframes([
        style({ opacity: 0 }),
        style({ opacity: 0.5 }),
        style({ opacity: 1 }),
      ])
    ),
  ]),
]),

].

Now, it works when adding a new input, but the animation also fires whenever the page loads, and by default, I have 3 inputs on the page.

How can I prevent the animation from firing on page load and to only happen whenever a new input is added?

I've tried adding a no-op animation on the parent (div), but it doesn't work.

    trigger(
        "blockInitialRenderAnimation",
        [
            transition( ":enter", [] )
        ]
    )

my HTML:

      <div
    *ngFor="let option of options.controls; let i = index"
    class="form-options"
  >
    <input
      type="text"
      placeholder="Add option {{ i + 1 }}"
      [formControlName]="i"
      (focus)="onOptionFocus(i)"
      [@input]="'in'"
    />
  </div>
like image 655
Yoni Segev Avatar asked Sep 01 '26 20:09

Yoni Segev


1 Answers

You can use disable animation using @.disabled binding. place this special binding on that element you want to disable animation. Then define isDisabled property inside your component then set isDisabled to true when you add form control

component.html

 <div [@.disabled]="isDisabled"
  *ngFor="let option of options.controls; let i = index"
  class="form-options" >
  <input
    type="text"
    placeholder="Add option {{ i + 1 }}"
    [formControlName]="i"
    (focus)="onOptionFocus(i)"
    [@input]="'in'"
  />
</div>

component.ts

export class SomeClass{
 isDisabled = true;

 addControl(){
  this.isDisabled = false;
  ....
 }
}
like image 70
Chellappan வ Avatar answered Sep 05 '26 12:09

Chellappan வ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!