No provider for Array

If a constructor of a service, component, or directive contains parameters, Angulars dependency injection tries to find a provider to get a value from it that it then passes to the constructor.

You don't have a provider registered for the type Item[].
Either

  • you register a provider
  • you add @Optional() before public items: Item[] so Angulars DI is allowed to ignore the parameter if it doesn't find a provider
  • you remove the parameter from the constructor.

Just initialize the array outside the constructor and create it in the constructor i.e.

export class Animal{    

    public animals: Animal[]; //initializing outside the constructor

    constructor(private animal: Animal) {

        this.animals = new Array<Animal>(); //creating inside the constructor

        animals.push(animal); //adding element to the array
    }        
}

Tags:

Angular