繁体   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