我有一个带有link指令的输入。我的问题是:当输入失去焦点时,是否有可能在指令中的链接函数内进行检测?
这是我的指示:
appDrct.directive('formattedDate', ['$filter', 'formatter', function ($filter, formatter) {
return {
link: function (scope, element, attrs, ctrl) {
ctrl.$parsers.unshift(function (viewValue) {
var val = element.val();
if(!val)
return viewValue;
var dateStr = $filter('date')(val,'dd.MM.yyyy');
if(dateStr == undefined)
return viewValue;
var parsed = viewValue;
if(**BLURRED**){
do something....
}
try
{
var dateParts = dateStr.split('.');
parsed = new Date(dateParts[2],dateParts[1]-1,dateParts[0]);
}
catch(e){
}
return parsed;
});
},
restrict: 'A',
require: 'ngModel'
};
}]);
JADE:
input.form-control(formatted-date='mybirth', name='birth' ,placeholder='jj.mm.aaaa', type="text", datepicker-popup="dd.MM.yyyy", ng-model="mybirth", is-open="$parent.opened[0]", close-text="Close")
一个例子:
答案 0 :(得分:1)
您也可以在element.on('blur', yourFn)
功能
link
答案 1 :(得分:0)
您可以使用ng-blur指令,请参阅此处http://plnkr.co/edit/Umx2czSRtgh3Cb2rKXYf?p=preview
<p class="input-group">
<input id="birth" formattedDate="mybirth" placeholder="dd.mm.yyyy" type="text" datepicker-popup="dd.MM.yyyy"
ng-model="mybirth" is-open="opened"
ng-blur="action()"
close-text="Close" class="form-control"/><span class="input-group-btn">
<button type="button" ng-click="open($event)" class="btn btn-default"><i class="glyphicon glyphicon-calendar"></i></button></span>
</p>