简体   繁体   中英

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). 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);    
        }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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