【问题标题】:ng-click not working in html tableng-click 在 html 表中不起作用
【发布时间】:2015-12-03 15:27:09
【问题描述】:

我正在尝试在表格内使用 ng-click,但它不工作,但在表格外它工作正常。

下面是 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>

点击Delete按钮deleteProduct方法不调用。

【问题讨论】:

  • 您是否尝试过在互联网上搜索 ng-repeat 创建子作用域?

标签: html angularjs angularjs-ng-click


【解决方案1】:

问题:

  1. 您正在遍历名为 rules 的集合/对象并将单个项目作为 rule .因此,您应该使用rule.yourProperty 访问每个项目的属性。产品是怎么来的?

  2. 对于 ng-click 函数参数,您不需要任何双花括号。只需传递对象的属性。 ng-click 指令以这种方式工作。

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

【讨论】:

    【解决方案2】:

    试试这个:-

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 2013-10-16
    相关资源
    最近更新 更多