AngularJS ng-click fires twice

后端 未结 18 1778
死守一世寂寞
死守一世寂寞 2020-12-03 07:10

I am using ng-click and it fires twice when I apply it to SPAN tag.

HTML

18条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 07:54

    I've had the same issue when dynamically injecting partial views in my SPA(Single Page Applications).The way I solved it was to store the content of my div in a local variable like this:

    var html = $("#leftContainer").html();
    

    Then empty out the div and reset its content like this:

    $("#leftContainer").empty();
    $("#leftContainer").append(html);
    

    Now you can add any additional html you have received from an AJAX call or dynamically constructed and lastly remember to re-compile your HTML to make it bound to angular:

    var entities = $("#entitiesContainer");
    
    var entity = "
     " + entityName + "
    " entities.append(entity); var leftContainer = angular.element(document.getElementById("entitiesContainer")); $compile(leftContainer)($scope);

提交回复
热议问题