我定义hostService
如下。场景是我先打电话hostService.addListener()
在控制器中,那么控制器可以通过以下方式发出消息$rootSceop.$emit
, hostService
应该可以处理它。
app.service('hostService', ['$rootScope', function ($rootScope) {
this.button = function () {
Office.context.ui.displayDialogAsync("https://www.google.com", {}, function () {});
}
this.addListener = function () {
$rootScope.$on("message", function (event, data) {
this.button()
})
}
但是,问题是上面的代码会引发错误:
TypeError: this.button is not a function
有谁知道如何修理它?
我通过创建一个 self 变量来解决这个问题this
对象,然后您可以从不同的范围调用它:
app.service('hostService', ['$rootScope', function ($rootScope) {
var self = this
this.button = function () {
Office.context.ui.displayDialogAsync("https://www.google.com", {}, function () {});
}
this.addListener = function () {
$rootScope.$on("message", function (event, data) {
self.button()
})
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)