Gaurav123
Gaurav123

Reputation: 5209

ng-click not working in html table

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

Answers (2)

Md Ashaduzzaman
Md Ashaduzzaman

Reputation: 4038

Problems :

  1. 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?

  2. 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
    };
}]);

jsFiddle

Upvotes: 1

vineet
vineet

Reputation: 14256

Try this:-

ng-click="deleteProduct(product.ID)"

Upvotes: 1

Related Questions