添加了AngularJS点击事件的动态内容无法处理添加的内容

时间:2022-11-07 12:06:49

I just started on AngularJS this week for a new project, and I have to come up to speed ASAP.

我本周刚开始使用AngularJS进行一个新项目,我必须尽快加快速度。

One of my requirements, is to add html content dynamically and that content might have a click event on it.

我的一个要求是动态添加html内容,内容可能有一个click事件。

So the code Angular code I have below displays a button, and when clicked, it dynamically adds another button. Clicking on the dynamically added buttons, should add another button, but I cannot get the ng-click to work on the dynamically added buttons

因此,我在下面的代码Angular代码显示一个按钮,当单击它时,它会动态添加另一个按钮。单击动态添加的按钮,应该添加另一个按钮,但我无法使用ng-click处理动态添加的按钮

<button type="button" id="btn1" ng-click="addButton()">Click Me</button>

The working code sample is here http://plnkr.co/edit/pTq2THCmXqw4MO3uLyi6?p=preview

工作代码示例在这里http://plnkr.co/edit/pTq2THCmXqw4MO3uLyi6?p=preview

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';

  $scope.addButton = function() {
    alert("button clicked");
    var btnhtml = '<button type="button" ng-click="addButton()">Click Me</button>';
    angular.element(document.getElementById('foo')).append((btnhtml));
  }
});
<!DOCTYPE html>
<html ng-app="plunker">

<head>
  <meta charset="utf-8" />
  <title>AngularJS Plunker</title>
  <script>
    document.write('<base href="' + document.location + '" />');
  </script>
  <link rel="stylesheet" href="style.css" />
  <script data-require="angular.js@1.0.x" src="//code.angularjs.org/1.3.0/angular.js" data-semver="1.3.0"></script>

</head>

<body ng-controller="MainCtrl">
  <p>Hello {{name}}!</p>
  <div id="foo">
  <button type="button" id="btn1" ng-click="addButton()">Click Me
  </button>
  </div>  
</body>

</html>

http://plnkr.co/edit/pTq2THCmXqw4MO3uLyi6?p=preview

http://plnkr.co/edit/pTq2THCmXqw4MO3uLyi6?p=preview

2 个解决方案

#1


41  

app.controller('MainCtrl', function($scope,$compile) {

    var btnhtml = '<button type="button" ng-click="addButton()">Click Me</button>';
    var temp = $compile(btnhtml)($scope);

    //Let's say you have element with id 'foo' in which you want to create a button
    angular.element(document.getElementById('foo')).append(temp);

   var addButton = function(){
       alert('Yes Click working at dynamically added element');
   }

});

you need to add $compile service here, that will bind the angular directives like ng-click to your controller scope. and dont forget to add $compile dependency in your controller as well like below.

你需要在这里添加$ compile服务,它会将像ng-click这样的angular指令绑定到你的控制器作用域。并且不要忘记在控制器中添加$ compile依赖项,如下所示。

here is the plunker demo

这是plunker演示

#2


1  

You could also bind the event to your new button.

您还可以将事件绑定到新按钮。

  $scope.addButton = function() {
    alert("button clicked");
    var btnhtml = '<button type="button">Click Me</button>';
    var newButt = angular.element(btnhtml);
    newButt.bind('click', $scope.addButton);
    angular.element(document.getElementById('foo')).append(newButt);
  }

Updated plunkr.

更新了plunkr。

#1


41  

app.controller('MainCtrl', function($scope,$compile) {

    var btnhtml = '<button type="button" ng-click="addButton()">Click Me</button>';
    var temp = $compile(btnhtml)($scope);

    //Let's say you have element with id 'foo' in which you want to create a button
    angular.element(document.getElementById('foo')).append(temp);

   var addButton = function(){
       alert('Yes Click working at dynamically added element');
   }

});

you need to add $compile service here, that will bind the angular directives like ng-click to your controller scope. and dont forget to add $compile dependency in your controller as well like below.

你需要在这里添加$ compile服务,它会将像ng-click这样的angular指令绑定到你的控制器作用域。并且不要忘记在控制器中添加$ compile依赖项,如下所示。

here is the plunker demo

这是plunker演示

#2


1  

You could also bind the event to your new button.

您还可以将事件绑定到新按钮。

  $scope.addButton = function() {
    alert("button clicked");
    var btnhtml = '<button type="button">Click Me</button>';
    var newButt = angular.element(btnhtml);
    newButt.bind('click', $scope.addButton);
    angular.element(document.getElementById('foo')).append(newButt);
  }

Updated plunkr.

更新了plunkr。