2017-01-22 56 views
1

我想选择父角色(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 !  
     } 
}); 

我应该为这个问题呢?

+0

你想要什么,为什么不使用NG式 –

+0

我用NG-风格:NG-风格=“product.name.length <= 32 { '的line-height':“50像素!重要的'}:{}“。但它不适合我! –

回答

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> 
+0

非常感谢。我测试了它,但它对我不起作用! –

+0

你能举个例子吗? –

+0

我自己测试了它,显然你不能在'ng-style'中使用'!important'关键字。我的解决方案是为您的自定义样式创建一个新的CSS类。我用'ng-class'更新了答案。这应该工作。 – basam96