AngularJs 和时刻.js不起作用
AngularJs and moment.js not working
我只想用Angular Js制作一个简短的网页来计算日期。时刻.js来自页面:http://momentjs.com/
这是当前代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Date Calculator</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"> </script>
<script src="moment.js"></script>
</head>
<body>
<script>
function personController($scope) {
moment();
$scope.year = moment().get('year');
$scope.month = moment().get('month');
$scope.day = moment().get('day');
}
</script>
<h1>Date Calculator</h1>
<div ng-app="" ng-controller="personController">
<form action="datecalc.html">
<h2>Startdate</h2>
<table>
<tr><td> <p>Day: <br><input name="day" type="number" size="20" maxlength="20" min="1" max="31" ng-model="day"></p></td>
<td> <p>Month: <br><input name="month" type="number" size="20" maxlength="20" min="1" max="12" ng-model="month"></p></td>
<td> <p>Year: <br><input name="year" type="number" size="20" maxlength="20" min="1980" max="4000" ng-model="year"></p></td>
</tr>
<tr>
<td>
Add/Substract
</td>
<td>Days: </td>
<td>Months: </td>
<td>years: </td>
</tr>
<tr>
<td> <select id = "todo">
<option value = "1">add</option>
<option value = "2">substract</option>
</select>
</td>
<td><input name="days" type="number" size="10" maxlength="100" ng-model="days"></td>
<td><input name="months" type="number" size="10" maxlength="100" ng-model="months"></td>
<td><input name="years" type="number" size="10" maxlength="100" ng-model="years"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
加载页面时,字段日、月和年应设置为当前日期。
知道为什么这不起作用吗?
此致敬意
我会把这作为一个正确的答案。
虽然这是一种非常糟糕的做角度的方法,你不应该养成这样做的习惯,但角度部分工作正常。问题是一时之需。
我不太了解时刻,但在这种情况下你甚至不需要它。您可以使用 javascript date 对象。这样。。。
var d = new Date();
$scope.year = d.getFullYear();
$scope.month = d.getMonth();
$scope.day = d.getDate();
还应该注意,当使用 getMonth() 时,它会将月份作为数组的索引返回,因此 January = 0,Feb = 1,12 月为 11。
编辑:如此快速地看,moment做同样的事情,他们也对"日"做同样的事情。Day 给你 1,这是数组的第二个索引,这将是第 2 天,这是正确的。用
moment().get('date');
相反,为您提供实际日期,然后只需在月份结果中添加 + 1。
相关文章:
- Js.erb VS按钮标记-不'不起作用.为什么?
- 为什么indexOf在这个js代码中不起作用
- 淘汰赛.JS'启用'长度绑定条件不起作用
- JS动态添加字段-删除按钮不起作用
- Knockout.JS标签在foreach内部不起作用
- 角度 JS + 重定向不起作用
- JS中的乘法不起作用
- Angular js+ng repeat+字母数字索引不起作用
- 角度Js ng-disabled不起作用
- 路径 lo 加载本地主机子文件夹中的文件不起作用.js仅在基路径中
- Jquery Carousel 不起作用(JS 问题)
- 验证方法在主干网中不起作用.js
- 应用程序.js在骨干中不起作用.js + 要求.js
- 路由在角度下不起作用.js - 没有控制台或语法错误
- Int from for 循环在函数中不起作用 - JS
- 为什么代码不起作用JS
- 为什么这个代码在iPad Pro上不起作用?(JS)
- .将Integer推入数组不起作用JS/JQuery
- 我的下拉列表不起作用-JS有问题
- Bootstrap弹出,粘贴不起作用.JS加载问题