I am trying to run a JavaScript code after Angular template generated, but I find the JavaScript codes don't run on dynamic generated views.
For example, in this code div2
is generated by clicking on div1
and alert("aaa")
runs by clicking on div1
, but does not run when div2
is clicked.
body of index.html
<div id="div1" ng-init="showDiv2 = false">
<div class="alertonclick" ng-click="showDiv2 =true" style="background-color: red"><br/></div>
</div>
<div id="div2" ng-if="showDiv2">
<div class="alertonclick" style="background-color: green"><br/></div>
</div>
and script in index.html:
<script src="~/index.js"></script>
index.js
$(document).ready(function () {
$(".alertonclick").click(function () {
alert("aaaa");
});
});