angularJS中的超时功能

编程入门 行业动态 更新时间:2024-10-11 09:30:03
本文介绍了angularJS中的超时功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在angularJS中实现一个简单的计时器。但是超时功能不起作用,尽管每个人都建议。

I am trying to implement a simple timer in angularJS. But timeout function is not working though it is suggested by everyone.

<!doctype html> <html lang="en"> <head> <script src="ajax.googleapis/ajax/libs/angularjs/1.2.15/angular.min.js"></script> <script src="ajax.googleapis/ajax/libs/angularjs/1.2.15/angular-sanitize.min.js"></script> </head> <body> <div ng-app="ss" ng-controller="mainCtrl"> {{timeInMs}} </div> <script> var myApp = angular.module("ss", ['ngSanitize']); myApp.controller('mainCtrl', function ($sce, $scope) { $scope.timeInMs = 10; var countUp = function() { $scope.timeInMs+= 500; $timeout(countUp, 500); } $timeout(countUp, 500); }); </script> </body> </html>

当我注释掉$ timeout(countUp,500);行,代码工作没有错误但计时器不起作用。我是否需要包含更多javascript文件?

When I comment out the " $timeout(countUp, 500);" line, the code works without error but timer doesn't work. Do i need to include any more javascript file?

提前致谢。

推荐答案

您必须将 $ timeout 服务注入控制器

You have to inject the $timeout service to the controller

var myApp = angular.module("ss", []); myApp.controller('mainCtrl', function ($sce, $scope, $timeout) { $scope.timeInMs = 10; var countUp = function () { $scope.timeInMs += 500; $timeout(countUp, 500); } $timeout(countUp, 500); });

<script src="ajax.googleapis/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="ss" ng-controller="mainCtrl"> {{timeInMs}} </div>

更多推荐

angularJS中的超时功能

本文发布于:2023-11-28 07:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641557.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:功能   angularJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!