繁体   English   中英

使用带有参数的函数的$ timeout,AngularJS fn不是函数错误

[英]AngularJS fn is not a function error using $timeout with a function with parameters

我正在创建一个可以编辑文本的网页,在您停止输入1秒后,它将自动保存您输入的内容。

目前我正在研究$ timeout细节。 当我调用没有参数的update方法时,我有它工作,但当我用params调用它时,我得到错误:

Error: fn is not a function $TimeoutProvider/this.$get</timeout/timeoutId<@http://localhost:63342/express_example/bower_components/angular/angular.js:14014 completeOutstandingRequest@http://localhost:63342/express_example/bower_components/angular/angular.js:4300 Browser/self.defer/timeoutId<@http://localhost:63342/express_example/bower_components/angular/angular.js:4601

为什么我在执行此操作时出现此错误:

timeout = $timeout(update(element, content), 1000);

但不是我这样做的时候:

timeout = $timeout(update, 1000);

显然我需要将params传递给update方法,因为我需要知道要更新的内容。

debounceUpdate($(this), data.content);

var debounceUpdate = function(element, content) {
    console.log('in debouce');
    if (timeout) {
      $timeout.cancel(timeout);
    }

    timeout = $timeout(update(element, content), 1000);
};

// Update an existing section
var update = function(element, content) {
    console.log('in update');
    console.log('section_id to update is '+element.data('sectionId'));
    console.log(content);
}

您的代码立即调用update并尝试将其返回值作为$timeout回调传递。 你真的想从$timeout处理程序调用update:

timeout = $timeout(function() {update(element, content);}, 1000);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM