![](/img/trans.png)
[英]How to use Angular 1 directive variable inside the Angular 2 component function
[英]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.