我试图编写一个指令来验证一个输入的值是否不大于另一个的值。它部分起作用,意味着该指令可以检测到较低的&更高的属性,但如何设置指令以正确解析插值?
问题是该元素具有使用隔离范围的另一个指令。另一个指令需要隔离范围,因此我得到下面列出的错误。我有什么选择?
错误:多个指令[fcsaNumber,isnotgreater(module:mdp)]询问 对于新的/隔离的范围:
HTML
<input name="participants" type="text" class="form-control" ng-model="CaseInput.participants" onkeypress='return event.charCode >= 48 && event.charCode <= 57' fcsa-number={{getValue(parts)}} required isnotgreater
lower="{{participants}}" higher="{{Employees}}"/>
指令
var isnotgreaterModule =angular.module('aaa.directives');
isnotgreaterModule.directive('isnotgreater', function() {
return {
restrict: 'A',
require: 'ngModel',
scope:{
lower: '&lower'
},
link: function ($scope, elem, attr, ngModelCtrl) {
//console.log("nodup link " + attr.noduplicate );
var lower = attr.lower;
var higher = attr.higher;
ngModelCtrl.$validators.isnotgreater = function(){
//return !scope.$parent.$parent[scope.noduplicate]();
var t=$scope.higher;
if (!$.isNumeric(lower) || !$.isNumeric(higher) ){
//not #'s, can't compare
ngModelCtrl.$setValidity('isnotgreater', true);
return true;
}
if (Number(lower) <= Number(higher)){
//lower so this is ok
ngModelCtrl.$setValidity('isnotgreater', true);
return true;
}else{
//not lower, this is invalid
//$scope.caseForm.$invalid = true;
ngModelCtrl.$setValidity('isnotgreater', false);
return false;
}
};
}
};
});