【问题标题】:angularjs How to make method call itself in controllerangularjs如何让方法在控制器中调用自己
【发布时间】:2016-02-19 13:08:41
【问题描述】:

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

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

我试过了:

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

希望得到帮助。

【问题讨论】:

  • var getExportPath = function() { .. } 然后getExportPath(); ..?
  • 但是我不能在构造函数之外调用函数。
  • 您的意思是要对函数进行递归调用吗?
  • 你可以使用$interval..

标签: javascript jquery angularjs frontend


【解决方案1】:

您的问题是 this 不是当前对象。通常,您希望在控制器开始时将 self 分配给 this 并使用 self 而不是 this

所以..

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-08
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 2017-08-01
    相关资源
    最近更新 更多