简体   繁体   English

角度的自定义验证指令

[英]custom validation directive in angular

I want to make a custom validation directive for my angular app. 我想为我的角度应用程序创建自定义验证指令。 the only problem is that I dont know how to get an value 唯一的问题是我不知道如何获得价值

<select class="form-control" ng-model="$parent.newSector" ng-options="item.id as item.name for item in sectors" not-empty-array="merchant.sectors"></select>

As you can see from my markup i have got an directive called notEmptyArray where i set an expression (it's the same as ng-model, just a different expression). 正如您从我的标记中看到的那样,我有一个名为notEmptyArray的指令,其中设置了一个表达式(它与ng-model相同,只是一个不同的表达式)。 How can i now access it in my directive? 现在如何在指令中访问它?

directive = function({
  require: "ngModel",
  link: function(scope, element, attributes, control) {
    //how to access the value of merchant.sectors (its an array in the scope)
  }
});

You would need to isolate the scope: 您需要隔离范围:

app.directive("notEmptyArray",function(){
    return {
        scope:{
            items:"=notEmptyArray"
        },
        link: function(scope, element, attributes, control) {
            /* anything you feed to "not-empty-array" would be accessible 
              inside items */
            console.log(scope.items);    
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM