![](/img/trans.png)
[英]How to run function with parameters using $timeout in AngularJS?
[英]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.