带角度的可点击引导行

时间:2013-07-15 15:52:26

标签: twitter-bootstrap angularjs

我有一张桌子,用引导程序设计。使用Angular.js填充此表的内容。如何使行可单击以便调用范围中的函数?

以下代码对我不起作用(ng-click部分):

表:

    <table class="table table-hover">
        <thead>
            <tr>
                <th>Name</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="ingredient in ingredients" ng-click="setSelected({{$index}});">
                <td>{{ ingredient.name }}</td>
                <td>{{ ingredient.status }}</td>
            </tr>
        </tbody>
    </table>

控制器:

$scope.setSelected = function(index) {
    $scope.selected = $scope.ingredients[index];
    console.log($scope.selected);
};

4 个答案:

答案 0 :(得分:43)

建议和fiddle

<tr ng-repeat="ingredient in ingredients" ng-click="setSelected();">
    <td>{{ ingredient.name }}</td>
    <td>{{ ingredient.status }}</td>
</tr>

<script>
var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {

    $scope.ingredients = [
        {'name': 'potato'},
        {'name': 'tomato'}
    ];

    $scope.setSelected = function() {
        $scope.selected = this.ingredient;
        console.log($scope.selected);
    };

}
</script>

答案 1 :(得分:2)

你可以在论证中传递成分

纳克单击= “的setSelected(成分)”

并在控制器中

   $scope.setSelected = function(my_ingredient) {
       $scope.selected = my_ingredient;
       console.log($scope.selected);
   };

答案 2 :(得分:1)

HTML:

<table class="table-hover">

CSS:

.table-hover > tbody > tr:hover {
    background-color: #f5f5f5;
}

如果还有什么可以选择:

HTML:

<tr ng-click="doSomething()">

CSS:

tr[ng-click] {
    cursor: pointer;
}

View JSFiddle Sample

答案 3 :(得分:0)

角度6

HTML:

<tr (click)="doSomething()">

CSS:

tr:hover {
    cursor: pointer;
}