簡體   English   中英

有什么辦法在角度4中使用閉合

[英]Is there any way to use closure in angular 4

我想在函數內部創建函數以在Angular 4中關閉。

當我嘗試執行以下操作時,出現“找不到名稱innerFn”錯誤

outerFn(){
  let a = "hello";
  innerFn(){
    console.log(a);
  }
}

有人可以幫我嗎?

您應該使用function關鍵字:

outerFn(){
  let a = "hello";

  var that = this; // use that to access the component
  function innerFn(){
    console.log(a);
  }

  innerFn(); // will log a
}

警告:如果您嘗試在此函數中使用this功能,由於關閉,它將不會引用您的組件。

暫無
暫無

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

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