javascript - Conditionally add attribute directive to the ng-repeat -
i know if possible add attribute directive elements inside of ng-repeat. have tried checking css class or id without luck.
example code
class itemnavigation { constructor(navigationservice ) { this.navigationservice = navigationservice; } } angular.module('core') .directive('itemnavigation', function ($timeout) { return { bindtocontroller: true, scope: { itemid: '=' }, controlleras: '$ctrl', restrict: 'a', link: function(scope, elem, attrs, ctrl) { $timeout(() =>{ elem.bind('click', function() { let noredirect = $(this).hasclass('no-redirect'); // approch doesnt work never finds class if(!noredirect) return ctrl.navigationservice.gotoitemdetails(ctrl.itemid) }) }) ; }, controller: itemnavigation }; });
example of html
<tr ng-repeat="item in $data track item.id" item-navigation item-id="item.id"> <td class="no-redirect">i dont want directive work here</td> <td>i want directive work here</td> <td>i want directive work here</td> <td class="no-redirect">i dont want directive work here</td> <td>i want directive work here</td> <td>i want directive work here</td> </tr>
i grateful help
you add event target code solve issues if need copy text tds , directive work when click outside of text. can remove timeout code.
please check following changes
<table> <tr ng-repeat="one in data" row-navigation item-id="one.id" class="table table-striped table-bordered"> <td class="no-redirect">dont alert when click here</td> <td><span class="no-redirect">dont when click on text only, works in td element</span></td> <td>alert when click here</td> </tr> </table>
and linking function should this
app.directive('rownavigation', function () { return { scope: { itemid: '=' }, controlleras: 'ctrl', restrict: 'a', link: function(scope, elem, attrs, ctrl) { elem.bind('click', function(event) { var noredirect = $(event.target).hasclass('no-redirect'); // work if(!noredirect) alert(ctrl.itemid) }) }, controller: function($scope) { this.itemid = $scope.itemid } }; });
Comments
Post a Comment