【问题标题】:Angular Timer not working角定时器不工作
【发布时间】:2023-03-19 01:33:01
【问题描述】:

我想显示一个倒计时。我一直在参考以下页面 http://siddii.github.io/angular-timer/

但我收到以下错误。

错误:指令计时器的隔离范围定义无效:@?

谁能告诉我我错过了什么。

index.html

<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript" src="angular-timer.min.js"></script>
<script type="text/javascript" src="app.js"></script>
</head>
<body>
<timer end-time="1451628000000">{{days}} days, {{hours}} hours, {{minutes}} minutes, {{seconds}} seconds.</timer>
</body>
</html>

app.js

var appModule=angular.module('app', ['timer']);

【问题讨论】:

  • 您使用的是哪个版本的 Angular?

标签: angularjs angular-timer


【解决方案1】:

试试这个。它正在工作:

我根据遇到的错误添加了 moment.jshumanizeDuration.js 库。

希望对你有帮助

<!DOCTYPE html>
<html>

  <head>
   <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.1/angular.min.js"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js"></script>
   <script src="https://raw.githubusercontent.com/EvanHahn/HumanizeDuration.js/master/humanize-duration.js"></script>
   <script src="https://raw.githubusercontent.com/siddii/angular-timer/master/dist/angular-timer.js"></script>
  </head>

   <body ng-app="app">

    <div ng-controller="ctrl">
      <timer end-time="1451628000000">{{days}} days, {{hours}} hours, {{minutes}} minutes, {{seconds}} seconds.</timer>
	</div>
	
	<script>
	   var app = angular.module('app',['timer']);
			
	   app.controller('ctrl', function($scope){});
	</script>
   </body>
</html>

【讨论】:

  • 感谢您的回复。
  • 我将 Angular 升级到 AngularJS v1.1.4 并包含 moment.js 和 humanize.js.Timer 现在正在显示,但计时器没有计时。只有当我刷新页面时计时器才会改变
  • 您有任何错误吗?您是否按原样尝试了上述工作 HTML 代码?也尝试使用 Angular 1.3.1
  • @JcDenton86 如何使用我的控制器范围而不是隔离范围?
  • @N.V.Prasad,这是一个新问题(在您的评论中)吗?我的回答解决了你的问题吗?
【解决方案2】:

您需要拉入正确的库才能使计时器工作。

bower install momentjs --save
bower install humanize-duration --save

现在在您的 HTML 中添加依赖项

<script type="text/javascript" src="bower_components/humanize-duration/humanize-duration.js"></script>

<script type="text/javascript" src="bower_components/momentjs/min/moment-with-locales.min.js"></script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 2011-10-18
    • 2013-03-27
    相关资源
    最近更新 更多