zefoy / ngx-dropzone-wrapper

Angular wrapper library for Dropzone
MIT License
174 stars 51 forks source link

Getting "no provider for DropzoneConfig" #63

Closed sefo closed 7 years ago

sefo commented 7 years ago

I moved from ng4 to ng5.

I used to import dropzone wrapper with forRoot(DROPZONE_CONFIG) but now that it's removed I import it exactly like in the docs.

The problem is in the component where I import DropzoneConfig to get config properties:

import { DropzoneConfig } from 'ngx-dropzone-wrapper';
...
  constructor(
    private dropzone: DropzoneConfig
  ) { }
...
    const acceptedFiles = this.dropzone.acceptedFiles;
    const maxFilesize = this.dropzone.maxFilesize;

Now I get

Error: StaticInjectorError[DropzoneConfig]: StaticInjectorError[DropzoneConfig]: NullInjectorError: No provider for DropzoneConfig!.

On the component side I can add:

providers: [ { provide: DropzoneConfig, useValue: DropzoneConfig } ]

This removed my error but I can't drop files anymore.

sconix commented 7 years ago

There is no more DropzoneConfig provided by the library. You need to use the injection token to get the config. You can check the directive constructor how it gets the global config using the injection token.

sefo commented 7 years ago

I checked the directive's source but I haven't seen an injection token. Since the behaviour has changed from previous versions, can we have a concrete example on how to inject DropzoneConfig in a component?

EDIT: I found an injection token (in the interfaces file?) but that doesn't help much. An example would be welcome.

sconix commented 7 years ago

The injection token is DROPZONE_CONFIG which is used to provide the global config. So if you look at the constructor of directive: constructor(private zone: NgZone, private renderer: Renderer2, private elementRef: ElementRef, private differs: KeyValueDiffers, @Optional() @Inject(DROPZONE_CONFIG) private defaults: DropzoneConfigInterface) {

You can see how to use it. I haven't added an example since injecting the global config into component goes to being very advanced and rare usage regarding this library. And more importantly its not specific to this library its about how Angular injection system works so documenting that does not belong to this librarys documentation. IMHO.