1

Hi I am using this context menu component: https://www.npmjs.com/package/ngx-contextmenu when I am binding the [contextMenu]="basicMenu" like this, and using the basicMenu with

@ViewChild(ContextMenuComponent)
public basicMenu:ContextMenuComponent

I get the following error: ExpressionChangedAfterItHasCheckedError: Previous Value: 'contextMenu:undefined'.Current value:'contextMenu[object Object]'

I know that the error is cause becuase the value is changed before the view is initialized in the middle of the process that runs the hooks.So I tried to run the change detection in the ngAfterViewInit with changeDetectorRef, however it doesnt work either...

MD10
  • 1,313
  • 3
  • 12
  • 35

1 Answers1

2

Try:

@ViewChild(ContextMenuComponent, { static: true }) public basicMenu: ContextMenuComponent;

I think there were some breaking changes with @ViewChild in Angular 9.

AliF50
  • 16,947
  • 1
  • 21
  • 37
  • can you explain what does it do ? it gives me only the read property – MD10 Feb 27 '20 at 15:13
  • 1
    This should make it available in `ngOnInit` instead of the `ngAfterViewInit` and no worries, I think you're on an older version of Angular. I am not sure how to fix this issue, maybe something with the library? – AliF50 Feb 27 '20 at 15:15
  • what do you mean by making it available on onInit? can you supply the full code ? excuse my ignorance – MD10 Feb 27 '20 at 15:17
  • 1
    https://stackoverflow.com/questions/56359504/how-should-i-use-the-new-static-option-for-viewchild-in-angular-8 Read the first answer – AliF50 Feb 27 '20 at 15:20
  • 1
    https://stackoverflow.com/questions/52922326/angular-6-expression-has-changed-after-it-was-checked Try the first answer here as well but I am not sure how clean/sustainable that is though. – AliF50 Feb 27 '20 at 15:21
  • thank you so much! I am in angular 7, so I cant use the static property ? – MD10 Feb 27 '20 at 15:29
  • 1
    Yes, I think it is not available in version 7. I think it was introduced in version 8. – AliF50 Feb 27 '20 at 15:29
  • 1
    I will try to upgrade.. meanwhile I used the ngAfterContentChecked hook and then forced another change detection whith changeDetectorRef – MD10 Feb 27 '20 at 15:32