No provider for array

I just started with Angular RC6. I import the HttpModule inside my @NgModule decorator.

However, I get this exception: There is no provider for Array!

How can i fix this?

- edit--: For this reason, this error is caused by:

constructor(private myService: CustomService, public items: Item[]) { }
+4
source share
3 answers

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

You do not have a provider registered for the type Item[].
Or

  • you will register a provider
  • you add @Optional()up public items: Item[]so that Corner DI can ignore the parameter if it does not find a provider
  • .
+9

,

export class Animal{    

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

    constructor(private animal: Animal) {

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

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

try using bindingsor providersin your code

eg

 @Component({
    selector: 'test-app',
    bindings: [ServiceName]
})
0
source

Source: https://habr.com/ru/post/1653424/


All Articles