0

I created a button with directive and need now on click to call the factory. The problem is when page opened it automatically call code. I need to do this with just one directive.

.directive("addcomment",addcomment);

         function addcomment(){
           var addComment = {
                link: link,
                restrict: "E",
                template: '<input type="submit" addcomments class="btn btn-default pull-right" value="Send" />'
           };

           return addComment;

           function link(scope, element, attrs){
              //RUN FACTORY
                };  
           }
         }

Any solution for this?

1
  • Why use a directive? You may as well just call a function in a controller using ng-click Commented Sep 10, 2015 at 19:58

1 Answer 1

3

So, in your

function link (scope, element, attrs) {
    //RUN FACTORY
}

you need to bind the factory execution to a click event.

function link (scope, element, attrs) {
    element.bind('click', function() {
        //Run the factory here
    });
}; 

I guess this should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.