1

I am adding a few buttons dynamically by injecting html using jquery. I want to be able to use ng-click on those elements but I do not know how to attach an angular click listener on it.

1
  • 1
    Don't use jQuery. Do it the Angular way, with directives. Then the ng-click will be compiled by Angular. Commented Oct 17, 2014 at 8:10

1 Answer 1

1

To attach a click listener on a button simply add ng-click="someMethod()" to the button:

<button ng-click="someMethod()">Button text</button>

you will need have to have a scope-method called someMethod().

for reference see the angular docs and note that the expression can be replaced by a method.

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

2 Comments

That does not work when I add content dynamially, for ex: $('#parentDiv').html('<button ng-click="someMethod()" >click me</button>'); This is what I want to achieve.
The reason it is not working is that you add your element in the controller. You should probably use a directive instead. Alternatively a service. You could also use pure javascript as in this link. What are you trying to accomplish, and why do you need to add it dynamically in the controller?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.