Search code examples
angularjsangularjs-directiveangular-directive

How to invoke a directive by using element name?


I have a angular directive:

app.directive('myDirective', function()
{
  return{
    restrict: 'AE',
    scope: {
        myCustomer: "&"
    },
    require: 'ngModel',
    link: function(scope, element, attr, ngModel){
        var oldVal;
        function fromUser(value){
            scope.myCustomer()(value, oldVal);
            oldVal = value;
            return value;
        }

        function toUser(value){
            return value;
        }

        ngModel.$parsers.push(fromUser);

        ngModel.$formatters.push(toUser);
    }
  }
}

At the moment I invoke this director by using attribute name and bind it to a function:

 <input type="text" my-directive="showInput" ng-model="user.name">

It works fine but what I want is to use element name, something like this:

<my-directive>

The problem is I don't know how to bind to a function as I do with the attributes.


Solution

  • You have to pass restrict: 'E' in directive options

    angular.module("image-management")
        .directive('myDirective', ($modal) => {
    
            return {
                restrict: 'E',
                scope:{
                  showInput: '&'  
                },
                template: '',
                link: function(){}
    })   
    
        <my-directive showInput="showInput" ></my-directive>