我正在尝试使用Angular js
实现自定义验证。
以下代码在 FireFox
上完美运行,但 IE 11
抛出错误
Expected ':'
对于行return range.valFunc({value});
有什么想法可以解决 IE
的问题吗?
谢谢。
指令:
crudApp.directive('customValidationFunction', function() {
return {
restrict: 'A',
require: 'ngModel',
scope: {
valFunc: '&customValidationFunction'
},
link: function(scope, element, attrs, controller) {
const normalizedFunc = function(modelValue, viewValue) {
const $value = modelValue || viewValue;
return scope.valFunc({$value});
};
controller.$validators.customValidationFunction = normalizedFunc;
}
};
});
验证函数:
//custom validation test
$scope.custValidationFunc = function($value) {
if (!$value) {
return true;
}
const lowVal = String($value).toLowerCase();
return lowVal.indexOf("arnold") === -1 && lowVal.indexOf("sylvester") === -1;
}
HTML:
<input type="text" class="form-control" id="i_position" name="i_position" aria-describedby="i_position_help" placeholder="Enter your Position" ng-model="position" custom-validation-function="custValidationFunc($value)">
最佳答案
scope.valFunc({value})
是 ES6 语法,IE 不支持它。您需要集成 Babel 或简单更改 scope.valFunc({value: value})
关于javascript - Angular JS 脚本适用于 Firefox,不适用于 IE 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52962191/