带角度的可点击引导行

Kla*_*aak 14 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>
Run Code Online (Sandbox Code Playgroud)

控制器:

$scope.setSelected = function(index) {
    $scope.selected = $scope.ingredients[index];
    console.log($scope.selected);
};
Run Code Online (Sandbox Code Playgroud)

mar*_*rko 43

建议和小提琴:

<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>
Run Code Online (Sandbox Code Playgroud)

  • 要分离控制器和模板,可以使用`ng-click ="setSelected(ingredient);"`和`$ scope.setSelected = function(item){...};`. (10认同)