0

I'm new in angular animations and currently following the Angular guide "Entering and leaving" part.

Currently I have something like that

<section
  class="row justify-content-center mb-2 mb-md-4"
  *ngFor="let site of sites"
  [@flyInOut]="site.state"
>

...repeatable content

</section>

and in component

import { Component, OnInit } from '@angular/core';
import { SiteService } from '../../Services/site.service';
import { Site } from './site';
import {
  trigger,
  state,
  style,
  animate,
  transition
} from '@angular/animations';

declare const $;

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss'],
  providers: [SiteService ],
  animations: [
    trigger('flyInOut', [
      state('in', style({transform: 'translateX(0)'})),
      transition('void => *', [
        style({transform: 'translateX(-100%)'}),
        animate(100)
      ]),
      transition('* => void', [
        animate(100, style({transform: 'translateX(100%)'}))
      ])
    ])
  ]
})
export class HomeComponent implements OnInit {
  sites: Site[] = [];

  constructor(
    siteService: SiteService,
    public name: string,
    public state: string = 'in'
  ) {
    this.sites = siteService.getSites();
  }

  ngOnInit(): void {
    $(function () {
      $('[data-toggle="tooltip"]').tooltip();
    });
  }

}

Animations module is imported in main module.

When site is loading it throws an error enter image description here

Sergey
  • 7,184
  • 13
  • 42
  • 85

1 Answers1

0

The problem is that you have a string in your constructor, and as you know, parameters in the constructor of Angular components are injected or provided, which is called Dependency Injection. Currently, it seems you have no provider for the string in your constructor. You have 2 ways of doing so:

  1. You need to provide a value for the string by using @Inject() decorator for the constructor parameter, like so:

    constructor(siteService: SiteService, @Inject(HOME_COMPONENT_NAME) name, ...)
    

    Where HOME_COMPONENT_NAME is an InjectionToken.

  2. Provide a value for the string by using provide(string, {useValue: ...}) in your @Component() decorator, like so:

    @Component({
        providers: [SiteService, provide(string, {useValue: 'your_string_here'})]
    })
    export class HomeComponent {
    

    Where 'your_string_here' is the value of your string.

I suggest you read more about Dependency Injection in Angular, and refer to this issue.

Amit Beckenstein
  • 1,220
  • 12
  • 20