gpt4 book ai didi

angularjs - Kendo AngularJS DatePicker 自定义验证

转载 作者:行者123 更新时间:2023-12-02 22:52:13 24 4
gpt4 key购买 nike

如何让自定义验证适用于 Kendo Angular 指令 DatePicker?我想实现以下示例,但使用 DatePickerAngular 指令版本:

KendoUI DatePicker validation when multiple date fields are on a page

如有任何帮助,我们将不胜感激。

最佳答案

这就是我如何使用自己的模板和验证基于 KEndo 的 Datepicker 实现自定义 Datepicker :

appModule.directive('nemoDatePicker', function () {
return {
restrict: 'E',
require: '?ngModel',
scope: {
'name': '@',
'text': '@',
'ngModel': '=',
},
templateUrl: '/App/nemo/nemoDatePicker.html',
link: function (scope, element, attrs, ngModel) {
},
controller: function ($scope) {
$scope.error = function (name) {
var s = $scope.editor[name];
return $scope.editor.$invalid && $scope.editor.$dirty ? "has-error" : "";
};
}
};
});


appModule.directive('kendoDateValidator', ['$sce', function ($sce) {
return {
restrict: 'A', // only activate on element attribute
require: '?ngModel', // get a hold of NgModelController
link: function (scope, element, attrs, ngModel) {
if (!ngModel) return; // do nothing if no ng-model

// Specify how UI should be updated
ngModel.$render = function () {
//element.html($sce.getTrustedHtml(ngModel.$viewValue || ''));
};

// Listen for change events to enable binding
element.on('blur keyup change', function () {
scope.$evalAsync(read);
});

read(); // initialize

function read() {

if (!element.val()) return;
const startTime = performance.now();
if (!isDate(element.val()))
{
//console.log(element.val() + " bad");
ngModel.$setValidity('date', false);
}
else
{
//console.log(element.val() + " good");
ngModel.$setValidity('date', true);
}
const duration = performance.now() - startTime;
//console.log("read took " + duration + " ms");
}

function isDate(x) {
var d = kendo.parseDate(x, "dd/MM/yyyy");
return d instanceof Date;
};

}
};
}]);
<div ng-form="editor">
<div class="Box" ng-class="error(name)">
<div class="SubTitle">
<div class="DirectiveIcon1">
<span class="glyphicon glyphicon-calendar"></span>
</div>
{{text}}
</div>
<input kendo-date-picker
culture="he-IL"
k-format="'dd/MM/yyyy'"
id="{{name}}"
name="{{name}}"
ng-model="ngModel"
required
kendo-Date-Validator
class="form-control"/>
<div>
<div>
<span class="help-block" ng-show="editor.$error.required && editor.$dirty">שדה חובה</span>
<span class="help-block" ng-show="editor.$error.date">יש להזין תאריך</span>
</div>
</div>
</div>
</div>

关于angularjs - Kendo AngularJS DatePicker 自定义验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25771015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com