Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:“输入错误”;这是……”;这不是一个函数_Javascript_Angularjs_This_Angularjs Rootscope - Fatal编程技术网

Javascript TypeError:“输入错误”;这是……”;这不是一个函数

Javascript TypeError:“输入错误”;这是……”;这不是一个函数,javascript,angularjs,this,angularjs-rootscope,Javascript,Angularjs,This,Angularjs Rootscope,我对主机服务的定义如下。我首先在控制器中调用senariohostService.addListener(),然后控制器可以通过$rootSceop.$emit,hostService来处理消息 app.service('hostService', ['$rootScope', function ($rootScope) { this.button = function () { Office.context.ui.displayDialogAsync("http

我对主机服务的定义如下。我首先在控制器中调用senario
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

有人知道如何修复它吗?

我解决了这个问题,用
这个
对象创建了一个自变量,然后您可以从不同的范围调用它:

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()
        })
    }

yout addListener中的“此”范围是您的全局范围,而不是本地范围。。你应该把你的作用域复制到一个变量中。每个函数都有自己的
上下文。在我看来,你并没有把
这个
绑定到任何东西上,所以在这种情况下,它可能是
窗口
。这个答案显示了解决这个问题的其他方法:@4castle uuuh!我的错!非常感谢你告诉我这个错误!你是巨大的;)