Nee*_*eel 2 validation angularjs angularjs-directive angularjs-scope ng-maxlength
我正在创建一个指令,该指令将带有文本类型输入的模板添加到视图。在此指令中,如果文本字段输入大于提供的最大长度设置,则尝试添加span类以进行错误通知。我有这样的代码:
<div ng-app="app">
<form name="userForm" ng-submit="processForm()" novalidate>
<div use-text-box name="xyz" ng-maxlength="10" required> </div>
<button type="submit" class="btn btn-success">Submit</button>
</form>
</div>
Run Code Online (Sandbox Code Playgroud)
我的指令是这样的:
var app = angular.module('app', []);
app.directive('useTextBox', function() {
return {
replace:true,
compile: function(element,attrs) {
var name = attrs.name;
var maxLengthError = attrs.hasOwnProperty('ngMaxlength') ? '<span ng-show="userForm.' + attrs.name + '.$error.maxlength" class="help-block">Text is too long. The limit is ' + attrs.ngMaxlength + ' characters.</span>' : '';
var htmlText = '<input type="text" name="' + name + '" ng-maxlength="' + attrs.ngMaxlength + '" required />' +
maxLengthError;
element.replaceWith(htmlText);
}
};
});
Run Code Online (Sandbox Code Playgroud)
但是在上面的代码中,该指令正在生成输入文本字段等。但是,如果最大长度大于10,则不会显示错误消息。我在做什么错?
这是上述示例的jsfiddle的链接:http : //jsfiddle.net/fB45J/3/
我不知道您是否只是在学习并尝试理解指令,但是您甚至不需要指令即可完成所需的操作。
这是没有指令的小提琴:http : //jsfiddle.net/mikeeconroy/fB45J/7/
<div ng-app="app">
<ng-form name="userForm" novalidate role="form" ng-controller="myFormCtrl">
<div>
<p class="text-muted">Enter in some text then remove it. You need to make it such that Angular sees the form as "dirty" and then tries validate the form.</p><br>
</div>
<div>
<input type="text" name="xyz" ng-model="xyz" maxlength="10" required>
<span ng-show="userForm.xyz.$dirty && userForm.xyz.$error.required && userForm.xyz.$invalid" style="color: #900;">This field is required!</span>
</div>
<br /> <br />
<button type="button" ng-click="processForm()" class="btn btn-success">Submit</button>
</ng-form>
</div>
Run Code Online (Sandbox Code Playgroud)
这是角度。它没有做任何事情,只是显示表单元素上的“ $ dirty”是如何工作的。
var app = angular.module('app', []);
app.controller('myFormCtrl',function($scope,$element){
$scope.form = $element.controller('form');
$scope.processForm = function(){
// set form to dirty
$scope.form.xyz.$dirty = true;
console.log('Processing!');
};
});
Run Code Online (Sandbox Code Playgroud)
编辑:这是使用您的指令方法的修复
http://jsfiddle.net/mikeeconroy/fB45J/8/
app.directive('useTextBox', function($compile,$timeout) {
return {
replace:true,
scope: false,
link: function(scope,element,attrs) {
var name = attrs.name;
var maxLengthError = attrs.hasOwnProperty('ngMaxlength') ? '<span ng-show="userForm.' + attrs.name + '.$error.maxlength" class="help-block">The limit is ' + attrs.ngMaxlength + ' characters.</span>' : '';
var htmlText = '<div><input type="text" id="' + name + '" name="' + name + '" ng-maxlength="' + attrs.ngMaxlength + '" ng-model="test_' + attrs.name + '" required>' + maxLengthError + '</div>';
$compile(htmlText)(scope,function(_element,_scope){
element.replaceWith(_element);
});
} // end link
};
});
Run Code Online (Sandbox Code Playgroud)
您需要注入$compile指令,然后使用它来编译HTML并以正确的作用域插入它。_element将是已编译的新元素。
$compile(htmlText)(scope,function(_element,_scope){
element.replaceWith(_element);
});
Run Code Online (Sandbox Code Playgroud)
编辑:这是仅使用compile指令属性的另一个示例
http://jsfiddle.net/mikeeconroy/dzP9L/1/
我想似乎您的示例与本示例之间的区别在于表单控制器的引入。
| 归档时间: |
|
| 查看次数: |
8174 次 |
| 最近记录: |