Исправляемая строка бутстрапа с угловым
У меня есть стол, оформленный с помощью бутстрапа. Содержимое этой таблицы заполняется с помощью 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);
};
Ответы
Ответ 1
Предложение и скрипка:
<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>
Ответ 2
Вы можете просто передать ингредиент в аргументе
нг кнопкой мыши = "setSelected (ингредиент)"
и в контроллере
$scope.setSelected = function(my_ingredient) {
$scope.selected = my_ingredient;
console.log($scope.selected);
};
Ответ 3
HTML:
<table class="table-hover">
CSS:
.table-hover > tbody > tr:hover {
background-color: #f5f5f5;
}
И если и что сделало tr выбираемым:
HTML:
<tr ng-click="doSomething()">
CSS:
tr[ng-click] {
cursor: pointer;
}
Просмотр примера JSFiddle
Ответ 4
Угловая 6
HTML:
<tr (click)="doSomething()">
CSS:
tr:hover {
cursor: pointer;
}