ng-click不工作角度1.2

时间:2016-12-28 09:31:34

标签: angularjs dynamic html-table

我的代码是:

var tr = $event.currentTarget;      
var el = angular.element("<tr class='detailed-document'><td colspan='3' ng-click='alert();'>Hello</td></tr>");      
var elements = document.getElementsByClassName("detailed-document");
while(elements.length > 0) {
    elements[0].parentNode.removeChild(elements[0]);    
}   
angular.element(tr).after(el);

1 个答案:

答案 0 :(得分:0)

你需要在这里添加$ compile服务,它会将像ng-click这样的angular指令绑定到你的控制器范围。如:

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

然后将其附加到HTML:

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

参考:ng-click not working in dynamically created content