簡體   English   中英

如何在jQuery函數中使用Angular 4變量

[英]How to use Angular 4 variable inside Jquery Function

這個問題已經在這里得到了回應。 但這似乎不適用於我。

我正在嘗試使用jQuery創建樹形結構。 問題是我不能在jQuery函數中使用聲明的angular 4變量。 這是代碼。

 employees = ["Mr. John", "Mr. Steve"]; ngOnInit() { (function($) => { function OrgChart($container, opts){ console.log(this.employees); } }); } 

我在控制台中看到一個錯誤,指出“針對ES3或ES5,在嚴格模式下不允許在塊內的函數聲明”

第一名( employees of undefined問題的employees of undefined

要綁定組件的“ this”,請對功能使用箭頭符號:

($) => { console.log(this.employees)}

而不是function($) { ... }

第二(“塊內不允許使用函數聲明”)

您可以在Angular組件的其他位置聲明其他內部函數,然后引用它:

ngOnInit() {
    // this declaration is nonsense to me, this will declare a function that is never called and exists only here, but anyway... 
    ($) => {
        // you can call OrgChart here : 
        this.OrgChart()
    } 
}

OrgChart($container, opts) { 
    console.log(this.employees); 
}

在啟動jquery塊之前,需要將角度分量參考存儲在另一個變量中。

export class PlanGruposComponent implements OnInit {
   group = 'Meu grupo';

   OnInit() {

       //reference of currect component object
       let temporaryThis = this; 

       $(document).ready(function () {
          console.log("Print : " + temporaryThis.group);
       });

   }
}

暫無
暫無

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

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