【发布时间】:2015-04-06 22:25:56
【问题描述】:
当试图在编辑模式下打开对话框时,验证消息会弹出所需的消息。但我需要实现的是,在编辑模式下打开对话框时,文本框将在对话框打开后立即包含值,但验证消息也存在。如果文本框中已经存在值,如何删除它。
下面是我如何使用它的 HTML 代码示例。
<div class="form-group col-lg-6" show-errors>
<label for="firstName">First Name</label>
<input type="text" class="form-control" id="firstName" ng-model="firstName" name="firstName" placeholder="First Name" style="width: 74%;" required>
<p class="help-block" ng-if="EditUserUserform.firstName.$error.required">Required</p>
</div>
<div class="form-group col-lg-6" show-errors>
<label for="lastName">Last Name</label>
<input type="text" class="form-control" id="lastName" ng-model="lastName" name="lastName" placeholder="Last Name" style="width: 74%;" required>
<p class="help-block" ng-if="EditUserUserform.lastName.$error.required">Required</p>
</div>
app.directive('showErrors', function() {
return {
restrict: 'A',
require: '^form',
link: function (scope, el, attrs, formCtrl) {
// find the text box element, which has the 'name' attribute
var inputEl = el[0].querySelector("[name]");
// convert the native text box element to an angular element
var inputNgEl = angular.element(inputEl);
// get the name on the text box so we know the property to check
// on the form controller
var inputName = inputNgEl.attr('name');
// only apply the has-error class after the user leaves the text box
inputNgEl.bind('blur', function() {
el.toggleClass('has-error', formCtrl[inputName].$invalid);
});
}
}
});
下面是我在正常模式下打开表单时的图像。
下图显示了当我在编辑模式下打开时。
该值已存在于其中,但一旦对话框打开,它就会显示验证消息。
【问题讨论】:
-
app.directive('showErrors', function(e) {e.preventDefault();} 请尝试,我不确定。
-
感谢您的快速回复,但它不起作用
标签: javascript jquery html angularjs angularjs-directive