0

dataSting Comes from database is like:

"<button id =\"play\" class=\"btn btn-simple\" (click)=\"testVideo('math', 'ytv')\"><span class=\"fa fa-play\"></span></button>"

Click function is not working when i clicked on button nothing is happened.

2
  • try using ng-click . Commented Feb 9, 2018 at 9:52
  • 1
    ng-click is a angular1 it will not work Commented Feb 9, 2018 at 9:55

1 Answer 1

0

That is not possible, without compiling the newly added code, for the reason that when you are replacing the content with the one returned from the database, it is only treated as plain HTML (it is not compiled and parsed).

When the (click)="testVideo(...)" is part of the HTML that is associated with a component, Angular first parses the HTML, and binds any handlers to the respective functions (which ultimately ends up as native javascript).

In the case when the HTML is added dynamically, during compile time Angular cannot identify the element and the associated click handler (because it is not yet present), without specifically tell angular to do so, hence why it does not work. Ultimately, when the button html is added, the (click) syntax is treated as plain html attribute.

Here's a plunker which demonstrates the above.

This solution https://stackoverflow.com/a/46578036/6666508 might be helpful if you absolutely need to retrieve syntax like the one you've presented from the database.

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.