angular.js - How does angular get a piece of html code and assign it to another place, so that the ng-click event in the html can take effect?
淡淡烟草味
淡淡烟草味 2017-05-15 16:56:19
0
3
671

I want to use an ng-click to get a piece of html. There is also an ng-click event in this html. I use js to find this piece of html and assign it to a pop-up layer. The ng-click event of this html is invalid. Okay, how to fix it?
$scope.readmore_maincomment = function(event){

    var html = $(event.target).parent(".subcomments").prev().html();           
        $(".subcomments_detail").find(".subcomment_con").html(html);

};

The code is an abbreviation. It is the content obtained in this way. The method may be used wrongly, and angular and jQuery are mixed. Who can tell me how to obtain this html content, and then the ng-click in the html can also take effect

淡淡烟草味
淡淡烟草味

reply all(3)
为情所困

Angular will traverse the dom at the beginning of startup, find out all the directives bound in html, compile and link, and then instructions like `v-click` will bind response events to the dom elements where they are located. If you directly insert the HTML string into the DOM, Angular will have no chance to parse the instructions in this string of HTML. In order to solve this problem, angular’s ​​built-in $compile service.

Usage:

  1. inject $compile service

  2. $compile(htmlstring or domelement)(scope)

  3. If compile htmlstring finally insert the return after the link into the dom

  • $compile() return a link function which bind the template to a scope

Official website document:

https://docs.angularjs.org/api/ng/service/$compile

仅有的幸福

The idea of ​​​​angularjs should try to avoid operating DOM. Your needs should be realized through other ways, and your goals can be achieved through data binding, or you can use ng-template to do it.

过去多啦不再A梦

The operations of DOM are all in the instructions. You have to write the DOM structure you currently want to operate as instructions, and then there is ele in the parameters of the link function for you to operate. The HTML you want to copy has ngclick. The same will take effect after copying.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template