AngularJS 应用程序中的 $timeout 未定义错误 [英] $timeout not defined error in AngularJS app
问题描述
我有以下代码:
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;
}]);
我在 Javascript 控制台中得到 $timeout not defined
.我是不是没有正确注入服务的依赖项?
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 应用程序中的 $timeout 未定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!