我正在尝试使用angular指令验证年龄(即大于18).谁能帮我验证年龄?
.directive('ageValid', ['$filter', function($filter) { return { restrict: 'A', require: 'ngModel', scope: { ageValid: '=' }, link: function(scope, element, attrs) { scope.$watch(attrs.ngModel, function(value) { var todayDate = new Date(), todayYear = todayDate.getFullYear(), todayMonth = todayDate.getMonth(), todayDay = todayDate.getDate(), dateFieldVal = value; console.log(dateFieldVal); var formattedDate = $filter('date')(dateFieldVal,'yyyy-MM-dd'); console.log(formattedDate); /*var isValid = (value.length === $scope.ngLength); ngModel.$setValidity($attrs.ngModel, isValid);*/ }); } } }])
Daniel.. 7
您不需要指令,您可以:
而你的控制器:
var today = new Date(); var minAge = 18; $scope.minAge = new Date(today.getFullYear() - minAge, today.getMonth(), today.getDate());
这是一个吸烟者
您不需要指令,您可以:
而你的控制器:
var today = new Date(); var minAge = 18; $scope.minAge = new Date(today.getFullYear() - minAge, today.getMonth(), today.getDate());
这是一个吸烟者