以角度选择父元素

时间:2017-01-22 18:12:47

标签: javascript angularjs

我想在角度中选择父级(searchres)并获得它的风格,但我不能这样做! 我的HTML是这样的:

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

我的代码就是这样:

$.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 !       
      }
});

我应该为这个问题做些什么?

1 个答案:

答案 0 :(得分:2)

在此示例中根本不需要Javascript代码。在某些条件下,您可以使用ng-class指令将类附加到div。

<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>
相关问题