AngularJS 表单验证 - 类型错误:无法设置未定义的属性“整数”
AngularJS Form validation - TypeError : Cannot set property 'integer' of undefined
我遵循了本教程(自定义验证),当我使用 AngularJS 自定义验证加载我的 html 表单时出现此错误。我正在使用一个名为"整数"的指令来验证我在表单中的数字输入。
我不知道为什么我会收到此错误,因为我在整数之前有另一个人格指令(excel 单元格验证),我没有收到任何错误。
这是我的号码输入:
<input type="number" ng-model="param.lineAct" class="form-control" min="0" integer />
我的指示:
var INTEGER_REGEXP = /^-?'d+$/;
adcAppDirectives.directive('integer', function() {
return {
require: 'ngModel',
link : function (scope, elm, attrs, ctrl) {
ctrl.$validators.integer = function (modelValue, viewValue){
if (ctrl.$isEmpty(modelValue)) { return false};
if (INTEGER_REGEXP.test(viewValue)) {return true};
return false;
};
}
};
});
错误的外观如下:
TypeError: Cannot set prpoerty 'integer' of undifined at link 127.0.0.1/Symfony/web/app_dev.php/js/…) input <type="number" ng-model="param.lineAct" class="form-control ng-pristine ng-valid" min="0" integer="">
编辑:这是整个指令javascript文件:
var adcAppDirectives = angular.module('adcAppDirectives', []);
var INTEGER_REGEXP = /^'-?'d+$/;
var EXCELCEL_REGEXP = /^[A-Z]+[0-9]+/;
var EXCELCELS_REGEXP = /^[A-Z]+[0-9]+:[A-Z]+[0-9]+/;
var EXCELCOL_REGEXP = /^[A-Z]+/;
adcAppDirectives.directive('integer', function()
{
return {
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
ctrl.$validators.integer = function (modelValue, viewValue) {
if (ctrl.$isEmpty(modelValue)){
return false;
}
if (INTEGER_REGEXP.test(viewValue)) {
return true;
}
return false;
}
}
};
});
adcAppDirectives.directive('excel-cel', function()
{
return {
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
ctrl.$validators.excel_cel = function (modelValue, viewValue) {
if (ctrl.$isEmpty(modelValue)){
return false;
}
if (EXCELCEL_REGEXP.test(viewValue)) {
return true;
}
return false;
};
}
};
});
adcAppDirectives.directive('excel-cels', function()
{
return {
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
ctrl.$validators.excel_cels = function (modelValue, viewValue) {
if (ctrl.$isEmpty(modelValue)){
return false;
}
if (EXCELCELS_REGEXP.test(viewValue)) {
return true;
}
return false;
};
}
};
});
adcAppDirectives.directive('excel-col', function()
{
return {
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
ctrl.$validators.excel_col = function (modelValue, viewValue) {
if (ctrl.$isEmpty(modelValue)){
return false;
}
if (EXCELCOL_REGEXP.test(viewValue)) {
return true;
}
return false;
};
}
};
});
和 html :
<form name="form" novalidate class="simple-form">
<div class="form-group">
<div class="col-lg-6">
Nom: <input type="text" ng-model="param.Name" class="form-control" />
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
Plage de paramètres: <input type="text" ng-model="param.paramTitle" class="form-control" excel-cels />
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
Ligne des traitements: <input type="number" ng-model="param.lineAct" class="form-control" min="0" integer />
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
Colonne de la DIR: <input type="text" ng-model="param.dirCol" class="form-control" excel-col />
</div>
</div>
<div class="form-group">
<button type="button" class="btn btn-success col-lg-offset-6 col-lg-2">Valider</button>
</div>
</form>
升级到最新版本的 AngularJS:formController.$validators
是 Angular 1.3 中的新功能
如果不能,请参阅您的文档。以下是 1.2 的表单指南:https://code.angularjs.org/1.2.28/docs/guide/forms
相关文章:
- 未捕获的类型错误:无法设置属性'背景'的未定义
- 将属性设置为未定义时未通知观察者
- 打字稿显示不能设置未定义的属性,如何解决
- Angular 2:在OnInit期间设置的属性在模板上未定义
- Highcharts无法设置属性'的值;要点':对象为null或未定义
- 无法设置属性'round'的未定义
- 未捕获类型错误:无法设置未定义的属性“操作”
- 快速会话 无法设置未定义的属性“id”
- 节点.js未捕获的异常类型错误:无法设置未定义的预处理 ''
- 如何处理 getJSON 调用上的“未捕获类型错误:无法设置未定义的属性'prop'”
- 在 JavaScript 对象中添加子对象时获取“无法设置未定义的属性'项目'”
- 在 javascript 中使用它获取未定义的设置对象的属性值
- IE错误:无法设置属性'的值;样式':对象为null或未定义
- 未捕获的类型错误:无法设置属性'vX'的未定义
- 无法调用方法'应用'关于为jquery谷歌地图插件设置方向的未定义
- JS原型无法设置属性'moveRight'的未定义
- 角度防止将模型设置为无效的未定义
- 无法设置/读取未定义的属性
- [可能的错误]数组中作为对象属性的值设置为未定义
- Javascript requestData未定义,尽管为JSON设置了var