Angular 动态创建和读取全局变量
在我的angular应用程序中,我使用一个框架来动态呈现html。我将调用下面的方法来呈现htmlAngular 动态创建和读取全局变量,angular,global-variables,Angular,Global Variables,在我的angular应用程序中,我使用一个框架来动态呈现html。我将调用下面的方法来呈现html Presto.layout(layoutJson, contentJson , document.getElementById('contentArea'), this.callbackFunction); 这将获取contentJson,并根据layoutJson构建html,然后注入contentAreadiv。 渲染完成后,将触发callbackFunction 问题 public cal
Presto.layout(layoutJson, contentJson , document.getElementById('contentArea'), this.callbackFunction);
这将获取contentJson
,并根据layoutJson
构建html,然后注入contentArea
div。
渲染完成后,将触发callbackFunction
问题
public callbackFunction(callbackID) {
this.anotherFunction(); // This will not work as the `this` is replaced with another object.
}
组件中的任何函数在回调函数中都不可用。回调函数中的this
对象包含与Presto js
相关的数据。我是否可以全局保存旧的this
并将其放入回调函数中。问题是回调函数中缺少“绑定”,请尝试以下操作:
Presto.layout(layoutJson、contentJson、document.getElementById('contentArea')、this.callbackFunction.bind(this))代码>您尝试过绑定吗?像Presto.layout(layoutJson、contentJson、document.getElementById('contentArea')、this.callbackFunction.bind(this))代码>很好。。成功了……)将此作为答案发布。