草庐IT

javascript - AngularJS fn is not a function error using $timeout with a function with parameters 错误

coder 2024-07-18 原文

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

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

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);

显然我需要将参数传递给更新方法,因为我需要知道要更新什么。

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 处理程序调用更新:

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

关于javascript - AngularJS fn is not a function error using $timeout with a function with parameters 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23391133/

有关javascript - AngularJS fn is not a function error using $timeout with a function with parameters 错误的更多相关文章

随机推荐