小编Ana*_*mar的帖子

Angular 9 - 本地参考,#,查看子,@ViewChild

我最近将我的项目升级到了 angular 9。

问题:-

我在模板中遇到了 Local Reference('#') 的问题。

我得到的值为“未定义”。

目标: 我试图在滚动时关闭 mat-select。

代码:

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>
Run Code Online (Sandbox Code Playgroud)

TS代码

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

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

    this.mySelect.close();
  }
Run Code Online (Sandbox Code Playgroud)

以上在 Angular 5 上运行良好,但是现在它在 9 上抛出错误。


Uncaught TypeError: Cannot read property 'close' of undefined
    at DetailComponent.onWindowScroll (detail.component.ts:1378)
    at DetailComponent_scroll_HostBindingHandler (detail.component.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 …
Run Code Online (Sandbox Code Playgroud)

javascript angular angular9

4
推荐指数
2
解决办法
1574
查看次数

标签 统计

angular ×1

angular9 ×1

javascript ×1