2

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

shrey shah
  • 91
  • 2
  • 11

3 Answers3

9

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
Allen
  • 3,134
  • 5
  • 29
  • 49
Shashwat Gupta
  • 5,071
  • 41
  • 33
0

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));
Eliseo
  • 50,109
  • 4
  • 29
  • 67
  • You didn't got my point, Yes I am using the dialogService to open the dialog component, my requirement is to also open that dialog component separately without any dialog box by router.navigate() and as I have initialize dialogref variable in the dialog component it generates this error when I open the same' component with router.navigate() – shrey shah Aug 17 '20 at 07:15
  • 1
    sorry, really I din't understand you.I think that you can use @Optional in constructor https://angular.io/api/core/Optional, e.g. `constructor(@Optional() private dialogRef: NbDialogRef) {}` But Really I don't know what it's the reason to inject the NbDialogRef. You can also pass data in state object and depending the data make one or another action, see https://netbasal.com/set-state-object-when-navigating-in-angular-7-2-b87c5b977bb – Eliseo Aug 17 '20 at 07:23
  • Yeah that worked! Thanks man.. You can also edit or give new answer so I can upvote it. :) – shrey shah Aug 17 '20 at 07:30
0

When I had the same problem it was that I was using the component without invoking it as a Dialog. That is why @Optional works.

rexsuecia
  • 331
  • 1
  • 2