我是Angularjs的新手,想要在页面加载后添加计时器。我在这里阅读了一些类似的问题,但我仍然无法解决我的问题。我在我的观点中添加了data-ng-init="init()"
。
这是控制器:
'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文件。任何帮助,将不胜感激。
答案 0 :(得分:1)
你可以尝试一下吗?
'use strict';
angular.module('tempApp')
.controller('MainController',['$scope','$timeout','dService', function ($scope,$timeout, dService) {
$scope.init = function(){
console.log("Safety checkStatus page load");
$timeout(function () {
$scope.callAtTimeout();
}, 1000);
}
$scope.callAtTimeout = function(){
console.log("Safety checkStatus ***");
}
$scope.init();
}]);
答案 1 :(得分:1)
<!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>
答案 2 :(得分:0)
'use strict';
angular.module('tempApp')
.controller('MainController',['$scope','$timeout','dService', function ($scope,$timeout, dService) {
$scope.init = function(){
console.log("Safety checkStatus page load");
$timeout(function () {
callAtTimeout()
}, 3000);
} }]);
function callAtTimeout(){
console.log("Safety checkStatus ***");
}
}]);
您可能还需要显式调用$ scope.init()
答案 3 :(得分:0)
添加&d;服务&#39;
.controller('MainController',['$scope','$timeout', 'dService', function ($scope,$timeout, dService) {
并使用
$timeout(callAtTimeout,3000);
答案 4 :(得分:0)
问题是$ scope.init()
中的额外括号$scope.init = function(){
console.log("Safety checkStatus page load");
$timeout(function () {
callAtTimeout();
}, 3000);
};
答案 5 :(得分:0)