NullInjectorError: No provider for NbDialogRef! in Nebular Dialog Service

Viewed 3667

I am using NbDialogService, and I am opening a component through NbDialogService, on that dialog component I have initialized Nbdialogref private dialogref: NbDialogRef<AddContactComponent>. Now I want to also open that component without dialog box, so when I open that I get these error NullInjectorError: No provider for NbDialogRef!. Any idea how to open a compoent in dialog box and same component with navigation or routing

2 Answers

Try using @Optional() in constructor to say the service is not required.

constructor(@Optional() private dialogRef: NbDialogRef<any>) {} 
    

Note:-
I used this when I used the same component in two places

  1. normal component page
  2. as a Model page

you need inject in constructor the NbDialogService, not the NgbDialogRef. dialogRef is a simple variable -generally you neen'd use a variable that belongs to the component, just a constant in your function "open"

constructor(private dialogService: NbDialogService){}

To open a component

const dialogref=this.dialogService.open(YourComponent, {
  context: {
    title: 'This is a title passed to the dialog component',
  })
dialogref.onClose.subscribe(name => name && this.names.push(name));
Related