Safari
Safari

Reputation: 3382

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

Upvotes: 0

Views: 215

Answers (1)

AlwaysALearner
AlwaysALearner

Reputation: 43947

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

Upvotes: 1

Related Questions