ng-click not working in dynamically created content

前端 未结 1 1309
再見小時候
再見小時候 2020-12-02 02:18

I have this function in Angular where I add a new slide with a ng-click in it.

var addSlide = function($scope, slideIndex, $event) {
  slideIndex++;
  var sl         


        
相关标签:
1条回答
  • 2020-12-02 03:06

    you need to add $compile service here, that will bind the angular directives like ng-click to your controller scope.Something like:

    var divTemplate = '..your div template';
    var temp = $compile(divTemplate)($scope); 
    

    Then append it to the HTML:

    angular.element(document.getElementById('foo')).append(temp);
    

    You can also bind the event to the div as following:

     var div = angular.element("divID");
     div.bind('click', $scope.addPhoto());
    
    0 讨论(0)
提交回复
热议问题