【发布时间】:2016-09-01 00:28:03
【问题描述】:
我的应用中的点击功能不适用于 angular js 中动态创建的元素。
这是我的代码:
HTML 代码
<div ng-app="myApp" ng-controller="myCtl">
<p>
Click is not working in table
</p>
<table border="1">
<tr ng-repeat="value in person.records">
<td>{{value.firstname}}</td>
<td>{{value.age}}</td>
<td ng-bind-html="value.button"></td>
</tr>
</table><br />
<br />
<p>
When I click on Click button, My function should be call like below
</p>
<button class="btn btn-primary" type="button" ng-click="trailClick()">Click</button>
</div>
JS 代码:在表格中,当我点击“点击按钮”时,没有得到我想要的功能。
var app = angular.module("myApp", ['ngSanitize']);
app.controller("myCtl", function($scope, $sce) {
$scope.trailClick = function() {
alert('Clicked ');
}
$scope.person = {
"records": [{
firstname: "John",
lastname: "Doe",
age: 50,
eyecolor: "blue"
}, {
firstname: "Dev",
lastname: "Raj",
age: 50,
eyecolor: "black"
}]
};
var cln_btn = '<button class="btn btn-primary" type="button" ng-click="trailClick()">Click</button>';
for (var x = 0; x < $scope.person.records.length; x++) {
$scope.person.records[x].button = $sce.trustAsHtml(cln_btn);
}
});
【问题讨论】:
-
我们需要更多信息。您是否在视图中包含了 .js 文件?控制台有错误吗?
-
不应该是console.alert()吗?
-
是的。我在我的应用程序中包含 angular.js 和 Sanitize.js 文件。控制台没有错误。请在以下路径中找到代码:jsfiddle.net/habibullah/vkby5fkt/14 谢谢
标签: angularjs