javascript - Angular 9 - Local Reference, #, View child, @ViewChild - Stack Overflow

I have recently upgraded my project to angular 9.Issue:-I am facing issue with Local Reference('#&

I have recently upgraded my project to angular 9.

Issue:-

I am facing issue with Local Reference('#') in template.

I get the value as 'undefined'.

Objective: I am trying to close mat-select on scroll.

Code:

Html

<mat-select (selectionChange)="showDataForLocation($event)"
            [(value)]="dataService.startinglocation"
            (openedChange)="selectPanelOpened($event)" 
            #mySelect>

      <mat-option  aria-selected="true" [value]="location.ExLocationName" 
                  *ngFor="let location of startingPointData?.ExLocation"> 
                    {{location.ExLocationName}}
      </mat-option>
</mat-select>

TS code

@ViewChild('mySelect', { static: true }) mySelect;

@HostListener('window:scroll', [])
  onWindowScroll() {

    this.mySelect.close();
  }

The above worked perfectly on Angular 5, however now it throws error on 9.


Uncaught TypeError: Cannot read property 'close' of undefined
    at DetailComponent.onWindowScroll (detailponent.ts:1378)
    at DetailComponent_scroll_HostBindingHandler (detailponent.ts:77)
    at executeListenerWithErrorHandling (core.js:21593)
    at wrapListenerIn_markDirtyAndPreventDefault (core.js:21635)
    at platform-browser.js:934
    at ZoneDelegate.invokeTask (zone-evergreen.js:400)
    at Object.onInvokeTask (core.js:40744)
    at ZoneDelegate.invokeTask (zone-evergreen.js:399)
    at Zone.runTask (zone-evergreen.js:168)
    at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:481)

I have recently upgraded my project to angular 9.

Issue:-

I am facing issue with Local Reference('#') in template.

I get the value as 'undefined'.

Objective: I am trying to close mat-select on scroll.

Code:

Html

<mat-select (selectionChange)="showDataForLocation($event)"
            [(value)]="dataService.startinglocation"
            (openedChange)="selectPanelOpened($event)" 
            #mySelect>

      <mat-option  aria-selected="true" [value]="location.ExLocationName" 
                  *ngFor="let location of startingPointData?.ExLocation"> 
                    {{location.ExLocationName}}
      </mat-option>
</mat-select>

TS code

@ViewChild('mySelect', { static: true }) mySelect;

@HostListener('window:scroll', [])
  onWindowScroll() {

    this.mySelect.close();
  }

The above worked perfectly on Angular 5, however now it throws error on 9.


Uncaught TypeError: Cannot read property 'close' of undefined
    at DetailComponent.onWindowScroll (detail.ponent.ts:1378)
    at DetailComponent_scroll_HostBindingHandler (detail.ponent.ts:77)
    at executeListenerWithErrorHandling (core.js:21593)
    at wrapListenerIn_markDirtyAndPreventDefault (core.js:21635)
    at platform-browser.js:934
    at ZoneDelegate.invokeTask (zone-evergreen.js:400)
    at Object.onInvokeTask (core.js:40744)
    at ZoneDelegate.invokeTask (zone-evergreen.js:399)
    at Zone.runTask (zone-evergreen.js:168)
    at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:481)

Share Improve this question edited Feb 26, 2020 at 10:19 Ramin eghbalian 2,6851 gold badge21 silver badges38 bronze badges asked Feb 26, 2020 at 9:28 Anand KumarAnand Kumar 1292 silver badges6 bronze badges 2
  • Try { static: false } instead of { static: true }. – Karmidzhanov Commented Feb 26, 2020 at 9:31
  • Karmidzhanov , { static: false } seems to have no effect. Getting same error. – Anand Kumar Commented Feb 26, 2020 at 9:52
Add a ment  | 

2 Answers 2

Reset to default 6

You should change the static flag to false.

// query results available in ngOnInit
@ViewChild('foo', {static: true}) foo: ElementRef;

OR

// query results available in ngAfterViewInit
@ViewChild('foo', {static: false}) foo: ElementRef;

More information on that topic the official Angular's migration guide: https://angular.io/guide/static-query-migration

try this:

@ViewChild('mySelect', { static: true }) mySelect: matSelect;

@HostListener('window:scroll', [])
  onWindowScroll() {

    this.mySelect.close();
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745657031a4638603.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信