我是 Angularjs 新手,想在页面加载后添加一个计时器。我在这里阅读了许多类似的问题,但仍然无法解决我的问题。我将 data-ng-init="init()"
添加到我的 View 中。
这是 Controller :
'use strict';
angular.module('tempApp')
.controller('MainController',['$scope','$timeout','dService', function ($scope,$timeout, dService) {
$scope.init = function(){
console.log("Safety checkStatus page load");
$timeout(callAtTimeout(),3000);
}
function callAtTimeout(){
console.log("Safety checkStatus ***");
}
}]);
jsfiddle:https://jsfiddle.net/Zusee/s6xy48t0/9/
这里 dService 是我要使用的单独的服务 js 文件。任何帮助,将不胜感激。
最佳答案
<!DOCTYPE html>
<html ng-app="tempApp">
<head>
<title>Test</title>
<script src="angular.min.js"></script>
</head>
<body ng-controller="MainController">
<script type="text/javascript">
'use strict';
angular.module('tempApp', [])
.controller('MainController',['$scope','$timeout', function ($scope,$timeout) {
$scope.init = function(){
console.log("Safety checkStatus page load");
$timeout(callAtTimeout, 3000);
};
function callAtTimeout(){
console.log("Safety checkStatus ***");
}
$scope.init();
}]);
</script>
</body>
</html>
关于javascript - Angularjs $timeout 不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39284921/