[英]Clicked on image jQuery - function doesn't work after append
I wrote this code for application which is using nodejs: 我为使用nodejs的应用程序编写了以下代码:
socket.on('daneTury', function (dataa){
var gracze = "";
$("#informacje").hide("slow");
$("#odpowiedziGraczy").empty();
for(i = 0; i < $scope.gracze.length; i++){
var lp = i + 1;
var panstwo = typeof dataa[$scope.gracze[i].text].panstwo !== 'undefined' ? dataa[$scope.gracze[i].text].panstwo : "";
var miasto = typeof dataa[$scope.gracze[i].text].miasto !== 'undefined' ? dataa[$scope.gracze[i].text].miasto : "";
var imie = typeof dataa[$scope.gracze[i].text].imie !== 'undefined' ? dataa[$scope.gracze[i].text].imie : "";
var rzecz = typeof dataa[$scope.gracze[i].text].rzecz !== 'undefined' ? dataa[$scope.gracze[i].text].rzecz : "";
var samochod = typeof dataa[$scope.gracze[i].text].samochod !== 'undefined' ? dataa[$scope.gracze[i].text].samochod : "";
gracze += "<tr><td>" + lp + "</td>" + "<td>" + $scope.gracze[i].text + "</td><td>" + panstwo + "</td><td><img id='wrong' src='img/wrong.png' data-ng-click='zle()' alt='wrong'></td><td>" + miasto + "</td><td><img id='wrong' src='img/wrong.png' alt='wrong'></td><td>" + rzecz + "</td><td><img id='wrong' src='img/wrong.png' alt='wrong'></td><td>" + imie + "</td><td><img id='wrong' src='img/wrong.png' alt='wrong'></td><td>" + samochod + "</td><td><img id='wrong' src='img/wrong.png' alt='wrong'></td></tr>";
}
$("#odpowiedziGraczy").append("<strong>Odpowiedzi reszty graczy:</strong><div class='table-responsive'><table class='table table-striped'><thead><tr><th>#</th><th>Nik</th><th>Panstwo</th><th></th><th>Misato</th><th></th><th>Rzecz</th><th></th><th>Imie</th><th></th><th>Samochod</th><th></th></tr></thead><tbody>" + gracze + "</tbody></table></div>");
$("#odpowiedziGraczy").show("slow");
});
My question is about: <img id='wrong' src='img/wrong.png' alt='wrong'>
. 我的问题是关于:
<img id='wrong' src='img/wrong.png' alt='wrong'>
。 Why when i clicked on this image function "zle" doesn't work? 为什么当我单击此图像功能时,“ zle”不起作用?
You have to $compile
elements you create dynamically, to make ng-click
or other angular directives work. 您必须
$compile
动态创建的元素,以使ng-click
或其他angular指令起作用。
After $("#odpowiedziGraczy").show("slow");
在
$("#odpowiedziGraczy").show("slow");
you should use: 您应该使用:
$compile(angular.element("#odpowiedziGraczy").contents())(scope);
Don't forget to inject $compile
service into your directive/controller. 不要忘记将
$compile
服务注入到指令/控制器中。
More about $compile
: https://docs.angularjs.org/api/ng/service/%24compile 有关
$compile
更多信息: https : //docs.angularjs.org/api/ng/service/%24compile
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.