Angular 动态创建和读取全局变量

Angular 动态创建和读取全局变量,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

在我的angular应用程序中,我使用一个框架来动态呈现html。我将调用下面的方法来呈现html

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))很好。。成功了……)将此作为答案发布。