2017-12-03 183 views
0

我想使用代碼美化爲特定的行着色。我從行號數據中獲取數據,並希望突出顯示它們。 如果我寫這在我的css文件:以角度訪問使用ID的第n個孩子

#file2 li:nth-child(n+1):nth-child(-n+11) { 
     background: #18b478; 
    } 

它的正常工作。但是,我無法弄清楚如何使用角度來實現這一點,我想根據我得到的行號動態地對CSS進行更改。這是我曾嘗試:

$("#file2 li:nth-child(n+1):nth-child(-n+11)").css('background', "#18b478"); 

這是我的html代碼:

<div class="container" style="margin-bottom: 50px"> 
<div class="col-sm-6"> 
    <pre> 
     <code class="prettyprint linenums" id="file1">***some code***</code> 
    </pre> 
</div> 
+0

使用angular通常意味着不要使用jQuery – charlietfl

+0

希望li:nth-​​child(n + 1)選擇每個li。我對這個選擇器有點困惑! –

回答

0

想象一下,你也行的顏色在數組中。 (用行數的長度)並且你在另一個數組中有你的行。

$scope.lines = linesarray 
$scope.colors = colorsarray 

代碼

<pre> 
    <code ng-repeat="line in $scope.lines"> 
     Line content: {{ line }} 
     Line number is: {{ $index }} 
     Associated color is: {{ colors[$index] }} 
    </code> 
    //make use of the $index that returns current index of the ng-repeat loop 
</pre> 

希望這有助於!