0

I have a question about this answers directive:

.directive( 'elemReady', function( $parse ) {
   return {
       restrict: 'A',
       link: function( $scope, elem, attrs ) {    
          elem.ready(function(){
            $scope.$apply(function(){
                var func = $parse(attrs.elemReady);
                func($scope);
            })
          })
       }
    }
})

And use:

<div elem-ready="someMethod()"></div>

My question is why do I need to pass someMethod plus () since in the directive we are turning it into a function (I still don't have a full grasp of what $parse does but I know it returns a function that can be called with a scope) and then we are calling it.

Thanks

LoyalPotato
  • 127
  • 1
  • 9
  • 1
    $parse will parse angularJS directive syntax. When you want to bind to an angular expression, you need to tell angular that this is a function call and not a value that is being bound. With scoped directives, you would do this with '&'. Since this directive doesn't have a scope, you need to use $parse. it's just the way the angularjs framework has decided to do things. – Asen Mitrev Oct 08 '21 at 07:47

0 Answers0