Can we bind ng-click to a CSS class?
I have loads of <td>s to bind and check if they are clicked then fire the click of the label in it, in order to give a maximum clickable area for touch devices.
I have done something like below.
var myApp = angular.module("myApp", [])
.controller("myController", ["$scope", "$timeout", function myController($scope, $timeout) {
$scope.checkRadio = function checkRadio($event) {
$timeout(function() {
angular.element($event.target).find("label").trigger('click');
}, 100);
};
}]);
.invisible-radio {
position: absolute;
display: inline;
opacity: 0;
width: 0;
margin: 0;
-webkit-appearance: none;
overflow: hidden;
}
.custom-radio-label {
display: inline-block;
position: relative;
padding-left: 20px;
vertical-align: text-top;
line-height: 20px;
cursor: pointer;
border-style: solid;
border-width: 0;
}
.custom-radio-label::before {
border-color: #7f7f7f;
border-radius: 50%;
background-color: #fff;
border-width: 2px;
content: "";
position: absolute;
top: 0;
left: 0;
width: 18px;
height: 18px;
border-style: solid;
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;
box-sizing: content-box;
}
.invisible-radio:checked+.custom-radio-label::after {
background-color: #337ab7;
border-radius: 50%;
top: 5px;
left: 5px;
content: "";
width: 12px;
height: 12px;
position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<table class="table table-hover text-center center-block" ng-app="myApp" ng-controller="myController">
<thead>
<tr>
<th>Option 1</th>
<th>Option 2</th>
</tr>
</thead>
<tbody>
<tr>
<td ng-click="checkRadio($event)">
<input id="option1" type="radio" class="invisible-radio" name="option" value="0" ng-model="model.Option" required />
<label class="custom-radio-label" for="option1"></label>
</td>
<td ng-click="checkRadio($event)">
<input id="option2" type="radio" class="invisible-radio" name="option" value="1" ng-model="model.Option" required />
<label class="custom-radio-label" for="option2"></label>
</td>
</tr>
</tbody>
</table>
But it seems overkill to go and decorate 100s of <td>s with ng-click attribute.
I am thinking if it is possible to bind ng-click to a common class that all my <td>s share.
It is certainly possible to do it with jQuery as below.
$(".someclassOnMyTD").click($event) {
$event.target.find("label").click();
}
But I am looking for an Angular way here. A directive possibly.
td's not generated in any way using a loop?tds.<td click-me>. imo using jquery in angular when necessary is fine.