Reconcile Angular.js and Bootstrap form validation styling

前端 未结 12 2051
刺人心
刺人心 2020-12-22 18:53

I am using Angular with Bootstrap. Here is the code for reference:

12条回答
  •  佛祖请我去吃肉
    2020-12-22 19:35

    Another solution: Create directive which toggles has-error class according to a child input.

    app.directive('bsHasError', [function() {
      return {
          restrict: "A",
          link: function(scope, element, attrs, ctrl) {
              var input = element.find('input[ng-model]'); 
              if (input.length) {
                  scope.$watch(function() {
                      return input.hasClass('ng-invalid');
                  }, function(isInvalid) {
                      element.toggleClass('has-error', isInvalid);
                  });
              }
          }
      };
    }]);
    

    and then simple use it in template

提交回复
热议问题