jQuery not working with ng-repeat results

柔情痞子 提交于 2019-11-28 20:48:23

The literal answer is because those handlers are bound at runtime, therefore .panel-heading doesn't exist. You need event delegation

$(".panel").on("click", ".panel-heading", function() {

Now, since you're using Angular, all DOM manipulation should be handled within a directive, not jQuery! You should be repeating either an ng-click handler, or a simple directive.

<div class="panel-heading" toggle-collapse my-cool-directive>

And the directive code:

.directive("myCoolDirective", function() {
    return {
        restrict: "A",
        link: function(scope, elem, attrs) {
            $(elem).click(function() {
                var target = $(elem).next(".panel-collapse");
                target.hasClass("collapse") ? target.collapse("show") : target.collapse("hide");
            });
        }
    }
});

When we use ng-repeat and need to trigger a jquery click event just try this it worked for me.

  $(document).on("click", ".className", function() {

   //your code here...

  });
ahmadalibaloch

You can run a jquery or any other javascript code after the angular finishes its rendering. For details see this answer : https://stackoverflow.com/a/20421291/2002079

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