[英]How to use Angular 4 variable inside Jquery Function
This question has already got some response here . 这个问题已经在这里得到了回应。 But this doesn't seem to be working for me.
但这似乎不适用于我。
I am trying to create a tree structure using jQuery. 我正在尝试使用jQuery创建树形结构。 The problem is I can not use a declared angular 4 variable inside a jQuery function.
问题是我不能在jQuery函数中使用声明的angular 4变量。 Here's the code.
这是代码。
employees = ["Mr. John", "Mr. Steve"]; ngOnInit() { (function($) => { function OrgChart($container, opts){ console.log(this.employees); } }); }
I see an error in console stating, "Function declarations are not allowed inside blocks in strict mode when targeting ES3 or ES5" 我在控制台中看到一个错误,指出“针对ES3或ES5,在严格模式下不允许在块内的函数声明”
employees of undefined
problem) employees of undefined
问题的employees of undefined
) To bind the "this" of the component, use the arrow notation for functions : 要绑定组件的“ this”,请对功能使用箭头符号:
($) => { console.log(this.employees)}
instead of function($) { ... }
而不是
function($) { ... }
You could declare your other inner function in another place in your Angular component, and then refer to it : 您可以在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);
}
You need store angular component reference in another variable before start jquery block. 在启动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.