我的表单中有2个日期字段,来自date
和todate
。我正在编写一个指令来比较这个日期字段。
如何从日期提交日期。我在onBlur
的{{1}}上调用我的自定义指令,现在需要从日期开始。表单设计如同填充。
toDate
答案 0 :(得分:0)
我的指令代码如下。
directives.directive('beforeDate', [
function() {
var link = function($scope, $element, $attrs, ctrl) {
var validate = function(viewValue) {
var comparisonModel = $attrs.beforeDate;
console.log(new Date(comparisonModel));
if(!viewValue || !comparisonModel){
ctrl.$setValidity('beforeDate', true);
}
ctrl.$setValidity('beforeDate', new Date(viewValue) > new Date(comparisonModel) );
return viewValue;
};
ctrl.$parsers.unshift(validate);
ctrl.$formatters.push(validate);
$attrs.$observe('beforeDate', function(comparisonModel){
console.log('comparisonModel '+comparisonModel)
return validate(ctrl.$viewValue);
});
};
return {
require: 'ngModel',
link: link
};
}
]);
并以我给出的形式
<span ng-repeat="education in userEducation">
<div class="row">
<div class="col-xs-6 col-sm-6 col-6">
<label>From date</label>
<input type="text" id="eduFromDate" name="eduFromDate" class="form-control" ng-model="education.fromDate" ui-date>
</div>
<div class="col-xs-6 col-sm-6 col-6">
<label>To date</label>
<div class="form-group has-success">
<input type="text" name="edutoDate" class="form-control" id="edutoDate" ng-model="education.toDate" ui-date before-date="eduFromDate">
<span class="error input-icon fui-alert" ng-show="historyForm.edutoDate.$error.beforeDate"></span>
</div>
</div>
</div>
</span>