Angularjs 在angular中的错误消息/表单验证
我是个新手。我有一个表格,我需要检查一个人是否14岁或以上。如果此人年龄不超过14岁,我需要显示一条错误消息 我的控制器中有一个功能来检查这一点。但我不知道如何为此编写错误消息 代码如下:Angularjs 在angular中的错误消息/表单验证,angularjs,forms,Angularjs,Forms,我是个新手。我有一个表格,我需要检查一个人是否14岁或以上。如果此人年龄不超过14岁,我需要显示一条错误消息 我的控制器中有一个功能来检查这一点。但我不知道如何为此编写错误消息 代码如下: <form name="myForm" > <input type="date" name="birthdate" ng-model="birthdate"> Is person older than 14: {{minAge(birthdate)}}<br/&g
<form name="myForm" >
<input type="date" name="birthdate" ng-model="birthdate">
Is person older than 14: {{minAge(birthdate)}}<br/>
<small class="error"
ng-show="myForm.birthdate.$error.minAge">
You must be 14 years or older
</small>
</form>
<script>
var app = angular.module("myApp", []);
app.controller('Ctrl', function ($scope) {
$scope.birthdate = new Date(2000,1,1);
$scope.minAge = function calculateAge(birthday) {
var ageDifMs = Date.now() - birthday.getTime();
var ageDate = new Date(ageDifMs);
return Math.abs(ageDate.getUTCFullYear() - 1970) > 14;
}
});
</script>
14岁以上的人:{{minAge(生日)}}
你必须是14岁或更大
var-app=angular.module(“myApp”,[]);
应用程序控制器('Ctrl',函数($scope){
$scope.birthdate=新日期(2000,1,1);
$scope.minAge=函数calculateAge(生日){
var ageDifMs=Date.now()-birth.getTime();
var ageDate=新日期(ageDifMs);
返回Math.abs(ageDate.getUTCFullYear()-1970)>14;
}
});
最好为该检查编写自定义指令。您可以重用现有代码,使其如下所示:
app.directive('minAge', function() {
return {
require: 'ngModel',
link: function(scope, element, attrs, ngModelController) {
var minAge = scope.$eval(attrs.minAge);
ngModelController.$validators.minAge = function(value) {
var ageDifMs = Date.now() - new Date(value);
var ageDate = new Date(ageDifMs);
return Math.abs(ageDate.getUTCFullYear() - 1970) > minAge;
};
}
};
});
其用途是:
<form name="myForm">
<input type="date" name="birthdate" ng-model="birthdate" min-age="14"> Is person older than 14: {{minAge(birthdate)}}
<br/>
<small class="error" ng-show="myForm.birthdate.$error.minAge">
You must be 14 years or older
</small>
</form>
14岁以上的人:{{minAge(生日)}
你必须是14岁或更大
演示:最好为该检查编写自定义指令。您可以重用现有代码,使其如下所示:
app.directive('minAge', function() {
return {
require: 'ngModel',
link: function(scope, element, attrs, ngModelController) {
var minAge = scope.$eval(attrs.minAge);
ngModelController.$validators.minAge = function(value) {
var ageDifMs = Date.now() - new Date(value);
var ageDate = new Date(ageDifMs);
return Math.abs(ageDate.getUTCFullYear() - 1970) > minAge;
};
}
};
});
其用途是:
<form name="myForm">
<input type="date" name="birthdate" ng-model="birthdate" min-age="14"> Is person older than 14: {{minAge(birthdate)}}
<br/>
<small class="error" ng-show="myForm.birthdate.$error.minAge">
You must be 14 years or older
</small>
</form>
14岁以上的人:{{minAge(生日)}
你必须是14岁或更大
演示:看看这个:
var-app=angular.module(“myApp”,[]);
应用程序控制器('Ctrl',函数($scope){
$scope.validDate=函数(){
var d=新日期();
返回新日期(d.getUTCFullYear()、d.getMonth()、d.getDate()-(14*365));
};
$scope.birthdate=新日期();
$scope.minValidDate=$scope.validDate();
$scope.checkDate=函数(){
$scope.isValidDate=(新日期($scope.birthdate).getTime()看这个:
var-app=angular.module(“myApp”,[]);
应用程序控制器('Ctrl',函数($scope){
$scope.validDate=函数(){
var d=新日期();
返回新日期(d.getUTCFullYear()、d.getMonth()、d.getDate()-(14*365));
};
$scope.birthdate=新日期();
$scope.minValidDate=$scope.validDate();
$scope.checkDate=函数(){
$scope.isValidDate=(新日期($scope.birthdate).getTime()