使用 AngularJS 单击事件添加的动态内容对添加的内容不起作用 [英] Dynamic content added with AngularJS click event not working on the added content
问题描述
这周我刚刚开始使用 AngularJS 进行一个新项目,我必须尽快跟上进度.
我的要求之一是动态添加 html 内容,并且该内容可能具有点击事件.
所以我下面的 Angular 代码显示了一个按钮,当点击时,它会动态添加另一个按钮.单击动态添加的按钮,应该添加另一个按钮,但我无法通过 ng-click 处理动态添加的按钮
工作代码示例在这里http://plnkr.co/edit/pTq2THCmXqw4MO3uLyi6?p=preview
var app = angular.module('plunker', []);app.controller('MainCtrl', function($scope) {$scope.name = '世界';$scope.addButton = function() {alert("按钮被点击");var btnhtml = '<button type="button" ng-click="addButton()">点击我</button>';angular.element(document.getElementById('foo')).append((btnhtml));}});
<html ng-app="plunker"><头><meta charset="utf-8"/><title>AngularJS Plunker</title><脚本>document.write('<base href="' + document.location + '"/>');<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>头部><body ng-controller="MainCtrl"><p>你好{{name}}!</p><div id="foo"><button type="button" id="btn1" ng-click="addButton()">点击我按钮>