angularjs - Angular 验证器和 ui-select

标签 angularjs ui-select

我是 Angular 新手。我正在使用 Angular-validator 来验证应用程序中的输入。我正在使用 ui-select 插件来显示选择列表。 我需要验证用户是否提交了表单并且他没有在选择列表中选择选项,然后我将显示所需的错误消息,就像 Plunkr 中的第一个输入一样。我相信验证是正确的,但它没有显示任何错误消息,我在网上搜索过,但在研发方面仍然没有运气。

任何帮助将不胜感激,提前致谢(为我的英语不好而道歉)

Problem 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/

相关文章:

javascript - 我的 JS 项目中哪里可以存储大量 "business"数据/对象?

angularjs - ngrepeat 别名为和 Controller 为

php - 使用 Rest POST 将数据从 angularjs 表单发送到 symfony2

css - 如何根据父级的名称属性更改CSS类

twitter-bootstrap-3 - 用户界面选择 : set width in Bootstrap grid system

angularjs - 在 angularjs 中从子状态 View 中隐藏父状态 View ,最佳实践?

java - 即使启用 CORS,POST 也会被阻止

javascript - Angular ui-select模型绑定(bind)

html - ui-select:如何更改所选元素的背景颜色?