NativeScript/nativescript-angular

NullInjectorError: No provider for ViewContainerRef!

myst-al opened this issue · 3 comments

i just want create modal dialog on service.ts
this is my full code

import { Injectable, ViewContainerRef } from '@angular/core';
import { ModalDialogOptions, ModalDialogService } from 'nativescript-angular/modal-dialog';
import { ModalError } from '~/modals/modal-error/modal-error.component';

@Injectable()
export class ModalDialog {

    constructor(private modal: ModalDialogService, private vcRef: ViewContainerRef) {  
    }

    public modal_error_connection(data) {
        let options: ModalDialogOptions = {
            context: {
                data: data,
                type: "connection"
            },
            fullscreen: true,
            viewContainerRef: this.vcRef
        };
        this.modal.showModal(ModalError, options).then((res: any) => {
        });
    }
}

and then error

JS: ERROR Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[NgForOf -> ViewContainerRef]:
JS:   StaticInjectorError(Platform: core)[NgForOf -> ViewContainerRef]:
JS:     NullInjectorError: No provider for ViewContainerRef!
JS: Error: NullInjectorError: No provider for ViewContainerRef!
JS:     at new ZoneAwareError (file: node_modules\@nativescript\angular\zone-js\dist\zone-nativescript.js:1298:0)
JS:     at NullInjector.push.../node_modules/@angular/core/fesm5/core.js.NullInjector.get (file: node_modules\@angular\core\fesm5\core.js:725:0)
JS:     at resolveToken (file: node_modules\@angular\core\fesm5\core.js:11918:0)
JS:     at tryResolveToken (file: node_modules\@angular\core\fesm5\core.js:11862:0)
JS:     at StaticInjector.push.../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (file: node_modules\@angular\core\fesm5\core.js:11764:0)
JS:     at resolveToken (file: node_modules\@angular\core\fesm5\core.js:11918:0)
JS:     at tryResolveToken (file:///data...

anyone can help me? please

A service doesn't have a ViewContainerRef. To do this you have to, for example, inject ViewContainerRef in your AppComponent and store it in the service for when you want to open a modal.

A service doesn't have a ViewContainerRef. To do this you have to, for example, inject ViewContainerRef in your AppComponent and store it in the service for when you want to open a modal.

thankyou so muchhhhh, work for me

It would be something like this

@Component({
    selector: 's2-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss']
})
export class AppComponent {
    title = 'admin-Servicio2000';

    constructor(private vcr: ViewContainerRef, private dm: DynamicComponentService) {
        this.dm.vcr = vcr;
    }
}