Explanation: the ng-click solution in the page dynamically generated by angularJS is invalid. angularjsng-click

Source: Internet
Author: User

Explanation: the ng-click solution in the page dynamically generated by angularJS is invalid. angularjsng-click

Today, I met this requirement. On the dynamic page written by myself, AngularJS is invalid and cannot click to respond to events. The following code and solutions are provided:

1. First, assign a value to the data on the page.

Var html = "<a href = 'javascript: void (0); 'ng-click = 'test () '> </a>"

2. Use the $ compile function to compile the above content

Var $ html = $ compile (html) ($ scope );

3. Insert compiled content into the page

$ ("Body"). append ($ html );

Complete

The complete version is as follows:

App. controller ('mermersctrl', function ($ scope, $ http, $ compile) {$ scope. test = function () {alert ('test ');} // TODO Dynamic html ng-click is invalid solution $ compile is passed in // The following sentence is to write the content on the page, first, assign the value of the content you write to html var html = "<a href = 'javascript: void (0); 'ng-click = 'test () '> </a> "// use $ compile to compile var $ html = $ compile (html) ($ scope); // Add it to the page, or any location you want to add .}); $ ("Body"). append ($ html );

In this way, ng-click can trigger the function.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.