我没有您提供的任何代码作为答案的依据,但这里有一个函数可以帮助您确定正确的年龄:
$scope.dateDiff = function(birthMonth, birthDay, birthYear) {
var todayDate = new Date(),
todayYear = todayDate.getFullYear(),
todayMonth = todayDate.getMonth(),
todayDay = todayDate.getDate(),
age = todayYear - birthYear;
if (todayMonth < birthMonth - 1)
{
age--;
}
if (birthMonth - 1 === todayMonth && todayDay < birthDay)
{
age--;
}
return age;
};
$scope.dateDiff(8,15,1963); // return as 50 as birthday has passed
$scope.dateDiff(9,15,1963); // return as 49 as birthday has not yet happened
在您的 HTML 中,您可以使用 ng-options 允许用户从下拉列表中选择日期。
Day <select ng-model="selectedDay" ng-options="day as day.date for day in days"></select>
Here is a full working example.
由于您是 Angular 新手,请务必花时间了解这段代码的工作原理。 ng-options 起初可能看起来有点混乱,但当你开始了解 Angular 原理时,应该不会有问题。