我是 Angular 新手。我正在使用 Angular-validator 来验证应用程序中的输入。我正在使用 ui-select 插件来显示选择列表。 我需要验证用户是否提交了表单并且他没有在选择列表中选择选项,然后我将显示所需的错误消息,就像 Plunkr 中的第一个输入一样。我相信验证是正确的,但它没有显示任何错误消息,我在网上搜索过,但在研发方面仍然没有运气。
任何帮助将不胜感激,提前致谢(为我的英语不好而道歉)
最佳答案
有一个指令可以解决这个问题,代码如下:
app.directive('showErrors', function($timeout) {
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
var inputName = inputNgEl.attr('name');
// only apply the has-error class after the user leaves the text box
var blurred = false;
inputNgEl.bind('blur', function() {
blurred = true;
el.toggleClass('has-error', formCtrl[inputName].$invalid);
});
scope.$watch(function() {
return formCtrl[inputName].$invalid
}, function(invalid) {
// we only want to toggle the has-error class after the blur
// event or if the control becomes valid
if (!blurred && invalid) { return }
el.toggleClass('has-error', invalid);
});
scope.$on('show-errors-check-validity', function() {
el.toggleClass('has-error', formCtrl[inputName].$invalid);
});
scope.$on('show-errors-reset', function() {
$timeout(function() {
el.removeClass('has-error');
}, 0, false);
});
}
}});
这里是文档:http://blog.yodersolutions.com/bootstrap-form-validation-done-right-in-angularjs/这是 ui-select 的示例:Plunkr
关于angularjs - Angular 验证器和 ui-select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28904034/