微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

angularjs – 将ngMessages与Angular 1.2一起使用

有谁知道是否有支持ngMessages的Angular 1.2的分支?

我很乐意使用它,但我对IE8有要求.

在此先感谢您的帮助.

解决方法

我已经更新了pleerock的答案,以处理具有for和when等属性的元素指令,例如ngMessages和ngMessage.您可以在此 github repo中找到相同的内容

angular.module('uiMessages',[]).directive('uiMessages',function() {

  return {
    restrict: 'EA',link: function(scope,element,attrs) {
      // hide all message elements
      var messageElements = element.find('ui-message,[ui-message]').css('display','none');
      // watch when messages object change - change display state of the elements
      scope.$watchCollection(attrs.uiMessages || attrs['for'],function(messages) {
        var oneElementAlreadyShowed = false;
        angular.forEach(messageElements,function(messageElement) {
          messageElement = angular.element(messageElement);
          var message = messageElement.attr('ui-message') || messageElement.attr('when');
          if (!oneElementAlreadyShowed && messages[message] && messages[message] === true) {
            messageElement.css('display','block');
            oneElementAlreadyShowed = true;
          } else {
            messageElement.css('display','none');
          }
        });
      });
    }
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>

<form name="userForm" ng-app="uiMessages" novalidate>
  <input type="text" name="firstname" ng-model="user.firstname" required />
  <ui-messages for="userForm.firstname.$error" ng-show="userForm.firstname.$dirty">
    <ui-message when="required">This field is mandatory</ui-message>
  </ui-messages>
  <br />
  <input type="text" name="lastname" ng-model="user.lastname" required />
  <div ui-messages="userForm.lastname.$error" ng-show="userForm.lastname.$dirty">
    <div ui-message="required">This field is mandatory</div>
  </div>
</form>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐