1st(
这个问题在这里已经得到了一些回应。但这似乎对我不起作用。
我正在尝试使用jQuery创建一个树结构。问题是我不能在jQuery函数中使用声明的角度4变量。这是代码。
employees = ["Mr. John", "Mr. Steve"];
ngOnInit() {
(function($) => {
function OrgChart($container, opts){
console.log(this.employees);
}
});
}
我在控制台中看到一个错误,指出"当针对 ES3 或 ES5 时,不允许在严格模式下的块内进行函数声明"
1st(employees of undefined
问题(
要绑定组件的"this",请使用函数的箭头表示法:
($) => { console.log(this.employees)}
而不是function($) { ... }
2nd("块内不允许函数声明"(
您可以在 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);
});
}
}