user4956265
user4956265

Reputation:

Calling a directive method on load of the template

<div ng-app="myApp">
    <div hello ng-click="hello()">directive</div>
</div>

I am calling a function onclick, is there a way i can call the method on the load of the template to load the data. I tried ng-init, but its not working.

    angular.module('myApp', [ ])
.directive('hello',function() {
      return {
        restrict: 'A',
        link: function($scope, element, attrs, controller) {
            element.on('click',function(){
                alert('clicked');
            });
            element.on("load", function(){
                alert("loaded");
            });
        }
      };
    }
  );

Upvotes: 0

Views: 738

Answers (1)

Vaibhav Pachauri
Vaibhav Pachauri

Reputation: 2671

Call the function in the 'compile' block of the directive instead of 'link' block. If you want to execute a function while loading of the directive(compiling phase). I hope that will do the trick.

Upvotes: 1

Related Questions