简体   繁体   中英

Clicked on image jQuery - function doesn't work after append

I wrote this code for application which is using 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'> . Why when i clicked on this image function "zle" doesn't work?

You have to $compile elements you create dynamically, to make ng-click or other angular directives work.

After $("#odpowiedziGraczy").show("slow"); you should use:

$compile(angular.element("#odpowiedziGraczy").contents())(scope);

Don't forget to inject $compile service into your directive/controller.

More about $compile : https://docs.angularjs.org/api/ng/service/%24compile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM