参数默认ngMessages指令

时间:2018-02-06 14:12:40

标签: angularjs angular-material ng-messages

为了对AngularJS Material输入做一些工作,我想知道我是否可以参数所有ngMessages指令而无需创建新的指令或在任何地方添加我的属性。

<nav mat-tab-nav-bar>
  <a mat-tab-link>
    Tab1 (click me to see the bug)
  </a>
  <a mat-tab-link>
    Tab2
  </a>
</nav>

是否有解决办法在所有ngMessages上自动添加 mdAutoHide 属性?

1 个答案:

答案 0 :(得分:1)

您可以专门为此编写指令。 这个简短的代码是错误的,因为很长一段时间我没有做角度js。 但是你可以看到这种方法:

DispatchQueue.main.async

//视图

//directive
angular.module('app').directive('messageHidden', function () {
    var tpl = '<div>ng-messages="form.number.$error" md-auto-hide="false"></div>'
    for(var input in $scope.inputs){
        tpl += '<div ng-message="'+input.type+'">'"+input.message+"'</div>';
    }

    return {
         restrict: 'E',
        replace: true,
        transclude: true,
       scope: {name: '@inputs'},
       template: tpl
   }
});



//ctrl
$scope.inputs = [
    {
        "type" : "required",
        "message" : "this is required"
    },
    {
        "type" : "min",
        "message" : "min is 0"
    },
    {
        "type" : "max",
        "message" : "max is 10"
    },

]