custom directive required params

329 views Asked by At

can we create a custom directive set as an element with required parameters , so that if these params are not provided by who ever would like to use it... then the directive must not work ???

**JS:**

 app.directive('customDirective', function() {
    return {
        restrict: 'E',
            scope : {
                data       :  "=data", ... 
            } , 
           templateUrl: function(element, attr) {
               // HTML file path 
             }, 
           ....
    }
 }); 

the case is now even if these params are not passed , the directive still works and injects the html in the view .

This is a generic question about directive not related to this specific case only .

1

There are 1 answers

1
Kevin F On

You could add your own validation in the link function

app.directive('customDirective', function() {
    return {
        restrict: 'E',
           scope : {
                data       :  "=data", ... 
           } , 
           templateUrl: function(element, attr) {
               // HTML file path 
           }, 
           link: function(scope, element, attrs){
               if(!data){
                   element.remove();
               }
           }
    }
 });

I'm not sure if there's a more official way though