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'); }});
您需要在
$compile此处添加服务,这样会将
angular directives诸如此类的内容绑定
ng-click到您的控制器范围。并且不要忘记
$compile像下面一样在您的控制器中添加依赖项。
这是小 plnkr演示
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)