簡體   English   中英

單擊角度按鈕后未觸發 ngOnDestroy

[英]ngOnDestroy not fired after button click in angular

HTML…………

<div class="form-group">
                  <button type="submit"

                          value="submit"
                          class="btn btn-secondary btn-float">Upload</button>
                </div>
              </div>

TS文件…………

export class testComponent implements  OnDestroy {

ngOnDestroy() {
    console.log("Fired ngOnDestroy");
  }

onClickSubmit() {

      this.subscription = this.service
        .create(test, url)
        .subscribe(
              res => {

                console.log('HTTP response', res)
              },
              err => {

                console.log("Error", err)
              },
              () => console.log('HTTP request completed.')
        );

  }
  }

我試圖在我的 ngondestroy() 沒有觸發之后點擊按鈕調用服務我不知道為什么它不起作用

當組件從 DOM 中移除時,框架會調用ngOnDestroy

您的示例中沒有任何內容表明按鈕單擊觸發銷毀。

如果我有以下組件,則單擊按鈕時將在子組件中調用ngOnDestroy

<child *ngIf="!deleted"></child>
<button (click)="deleted = true">Trigger destroy in child</button>

演示: https : //stackblitz.com/edit/angular-fxvj1n

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM