ng-click not working for dynamically inserted html got from the http.get call in angular js

痞子三分冷 提交于 2019-12-24 14:19:55

问题


Hi I am using angular js for my php project. I am using ngclick for anchor link which is like this

<a href="http://localhost/mediaads/signup" ng-click="showregister($event)">Don't have an account? Create One</a>    

when clicks on the link i will call method showregister in controller and replace the html by calling http get method.

$http.get(baseUrl+'signup').success(function(res){$(elem).replaceWith(res);})    

res is the html data and again it has ng click in the html

Dynamically got html data has again ng clicks in them. Those ng clicks are not working

Can anyone tell me how to make ngclick work for dynamic elements.


回答1:


HTML that is added dynamically like this must manually be linked with a scope.

To do this inject and use the $compile service:

$http.get(baseUrl + 'signup').success(function(res) {
  $(elem).replaceWith($compile(res)($scope));
});

Demo: http://plnkr.co/edit/E0neYHsVnmHKn7goLqL4?p=preview



来源:https://stackoverflow.com/questions/27838634/ng-click-not-working-for-dynamically-inserted-html-got-from-the-http-get-call-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!