Reputation: 5209
I am trying to use ng-click inside table but its not working, however outside the table its working fine.
Below is HTML
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Section</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="product in products">
<td>{{product.ID}}</td>
<td>{{product.Name}}</td>
<td>{{product.Section}}</td>
<td><input type="button" name="name" value="Delete" class="btn btn-danger" ng-click="deleteProduct({{product.ID}});" /></td>
</tr>
</tbody>
</table>
On click of Delete
button deleteProduct
method doesn't call.
Upvotes: 0
Views: 4828
Reputation: 4038
Problems :
You are traversing through a collection/object named rules and getting single item as rule. So you should access each single items properties with rule.yourProperty
. How did product come here?
You don't need any double curly braces to for ng-click function parameters. Simply pass the property of the object. ng-click directive works this way.
HTML :
<div ng-controller="mainCtrl">
<table class="table table-striped">{{msg}}
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Section</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="rule in rules">
<td>{{rule.ID}}</td>
<td>{{rule.Name}}</td>
<td>{{rule.Section}}</td>
<td><input type="button" name="name" value="Delete" class="btn btn-danger" ng-click="deleteProduct(rule.ID)" /></td>
</tr>
</tbody>
</table>
</div>
angular :
angular.module('myapp', []).controller('mainCtrl', ['$scope', function($scope){
var data = [
{
ID : "1",
Name : "A1",
Section : "A1"
},
{
ID : "2",
Name : "A2",
Section : "A2"
},
{
ID : "3",
Name : "A3",
Section : "A3"
},
{
ID : "4",
Name : "A4",
Section : "A4"
}
];
$scope.rules = data;
$scope.deleteProduct = function(id){
alert(id);
// delete your item here
};
}]);
Upvotes: 1