简体   繁体   中英

Select parent element in angular

I want to select parent (searchres) in angular and get style to it but I can't do it ! My HTML is this :

    <div ng-repeat="product in products" class="searchres">
         <a href="#">
              <img src="{{product.path}}" class="img-thumbnail" alt="{{product.name)}}" />
              {{product.name}}                            
         </a>
    </div> 

And my code is this :

$.each($scope.products, function(index, value) {
      var namePro = value.name; 
      if (namePro.length <= 32) {
          $("body").find(".searchres").css("line-height","50px!important");// is wrong !       
          value.parentNode.css("line-height","50px!important"); // is wrong !       
      }
});

what should I do for this problem ?

There is no need for Javascript code at all in this example. You can use the ng-class directive to attach a class to your div under certain conditions.

<div ng-repeat="product in products" class="searchres" ng-class="product.name.length <= 32 ? 'your-class-name' : ''">
     <a href="#">
          <img src="{{product.path}}" class="img-thumbnail" alt="{{product.name)}}" />
          {{product.name}}                            
     </a>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM