AngularJS app中的$ timeout未定义错误 [英] $timeout not defined error in AngularJS app
本文介绍了AngularJS app中的$ timeout未定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
app.factory('Position', ['$timeout', function() {
var position = {
latitude: 44,
longitude: 26
};
console.log("Timeout started");
$timeout(function() {
position.latitude += 15;
position.longitude += 15;
}, 2000);
return position;
}]);
我得到 $ timeout未定义
in Javascript控制台。我没有正确地注入服务的依赖吗?
And I get $timeout not defined
in Javascript console. Am I not injecting the dependency of the service correctly ?
推荐答案
你没有注入 $ timeout
。它应该如下。
You did not inject $timeout
. It should be as follows.
app.factory('Position', ['$timeout', function($timeout) {
...
}]);
这种声明可确保在JavaScript代码缩小时正确识别服务。有关如何帮助缩小的更多信息,请参阅关于缩小的注意事项和声明AngularJS模块以进行缩小
Declaration this way ensures that services are correctly identified when your JavaScript code gets minified. For further information on how this helps minification, see A Note on Minification and Declaring AngularJS Modules For Minification
如果缩小不在您的计划中(例如快速测试),您可以简单地使用
If minification is not in your plans (e.g for quick test), you can simply go with
app.factory('Position', function($timeout) {
...
});
这篇关于AngularJS app中的$ timeout未定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文