繁体   English   中英

将jQuery与来自angular的动态创建的元素一起使用不起作用

[英]Using jquery with dynamically created elements from angular doesnt work

我试图使用jquery来操纵由angular创建的元素,但是我无法使其工作。 我想知道是否有人可以帮助我。 谢谢

这是HTML

<div class="patients">
    <tbody ng-repeat="patient in patients">
        <tr>
            <td>{{patient.name}}</td>
            <td>{{patient.number}}</td>
            <td>{{patient.date}}</td>
            <td id="item-{{$index}}">{{patient.reminded}}</td>
            <div class="sendreminder">
                <td>
                    <a href="" class="btn btn-info btn-sm sendreminder" style=" background-color: #00e699; border-color:#00e699; " ng-click="post($index) " "$parent.selected = $index" id="button-{{$index}}">
                        <span class="glyphicon glyphicon-send"></span> Request Payment
                    </a>
                </td>
            </div>
            <td>
                <a href="" style="text-decoration:none; color:inherit; scale: 4" class="pe-7s-info">
                </a>
            </td>
        </tr>

    </tbody>
</div>

这是jQuery

$(function() {
$('.patients').on('click', ".sendreminder",function(e){
         alert('worked');
    });

});

动态创建新元素后,应立即调用该代码,因为该代码为具有。Patient不是新类的实际元素 (当您调用函数时)设置了处理程序。

ng-repeat每次检测到更改都会重新创建DOM(因此,所有附加事件都将消失)。 因此,要在ng-repeat完成后重新附加事件,您可以

<tbody ng-repeat="patient in patients" ng-init="$last && ngRepeatFinish()">

$lastng-repeat的最后一项,则将其设置为true

在您的控制器中,创建ngRepeatFinish()函数

$scope.ngRepeatFinish = function(){
    $('.sendreminder').click(function(e){
         alert('worked');
    });
}

您还可以为此制定更好的自定义指令,但这足以快速解决问题。 请参阅以获取具有自定义指令的解决方案

我建议您使用Angular而不是Jquery

在下面添加了两种方法

 //using Jquery $('.patients').on('click', ".sendreminder", function(e) { alert('from JQuery'); }); function TestCtrl($scope) { $scope.patients = [{ name: 'one', number: 1, date: '2016-08-16', reminded: true }, { name: 'two', number: 2, date: '2016-08-16', reminded: true }, { name: 'three', number: 3, date: '2016-08-16', reminded: false }]; //using angular $scope.post = function(i) { alert('from Angular'); var selectedPatient = $scope.patients[i]; console.log(selectedPatient); }; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div ng-app> <div class="patients" ng-controller="TestCtrl"> <table> <thead> <tr> <th>Name</th> <th>Number</th> <th>Date</th> <th>Reminded</th> <th>Request</th> <th>Info</th> </tr> </thead> <tbody> <tr ng-repeat="patient in patients"> <td>{{patient.name}}</td> <td>{{patient.number}}</td> <td>{{patient.date}}</td> <td id="item-{{$index}}">{{patient.reminded}}</td> <td> <a href="" class="btn btn-info btn-sm sendreminder" style="background-color: #00e699; border-color:#00e699;" ng-click="post($index)" id="button-{{$index}}"> <span class="glyphicon glyphicon-send"></span> Request Payment </a> </td> <td> <a href="" style="text-decoration:none; color:inherit; scale: 4" class="pe-7s-info">test </a> </td> </tr> </tbody> </table> </div> </div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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