angularjs如何在控制器中调用方法本身

时间:2016-02-19 13:08:42

标签: javascript jquery angularjs frontend

所以,我的问题是,如何使控制器方法调用自身(在控制器构造函数中定义),例如:

this.getExportPath = function() {
    setTimeout(function() {
        console.log(1);
        return getExportPath();
    }, 1);
}

我试过了:

this.getExportPath();
getExportPath();
return getExportPath();
return this.getExportPath();

帮助会受到影响。

1 个答案:

答案 0 :(得分:4)

您的问题是this不是当前对象。通常,您希望在控制器的begginng处将self分配给this并使用self代替此

所以..

var self = this;
self.getExportPath = function(){
    setTimeout(function(){
        self.getExportPath();
    },1)
}