2015-04-01 38 views
0

我有一些產品,我想這樣並排顯示。 enter image description here爲什麼angularJS循環渲染框在新的行

這個硬編碼的代碼工作正常。

<div> 
    <div class="box"></div> 
    <div class="box"></div> 
    <div class="box"></div> 
    <div class="box"></div> 
    <div class="box"></div> 
    <div class="box"></div> 

但是,當我試圖做到同樣的事情angularJS循環。

<div ng-repeat="product in Products"> 
    <div class="box"> 
     //i will fill the details here. 
    </div> 
</div> 

我得到了這個結果。 enter image description here

這是我的CSS類。

.box { 
padding : 5px; 
display : inline-block; 
min-width: 100px; 
min-height: 50px; 
background-color: red; 
} 

我需要做什麼改變,如果屏幕寬度已滿,產品可以並排顯示,並在下一行顯示。

回答

3

固氮菌原來的問題

ng-repeat應該是<div class="box">

<div> 
    <div ng-repeat="product in Products" class="box"> 
     //i will fill the details here. 
    </div> 
</div> 

說明是你所做錯

通過這樣做,你正在創建重複新<div class="box">每個product in Products

您之前的做法意味着您要爲每個product in Products創建一個新的容器元素。

簡單容易出錯。

修理你的CSS樣式

要實現你在OP顯示風格,你將要保證金添加到您的重複的元素也添加它們之間有一些間隔。 這樣做的改變:

.box { 
    padding : 5px; 
    display : inline-block; 
    min-width: 100px; 
    min-height: 50px; 
    background-color: red; 
} 

.box { 
    margin-right : 5px; 
    display : inline-block; 
    min-width: 100px; 
    min-height: 50px; 
    background-color: red; 
} 
+0

其工作正常部分。但是這裏沒有填充效果。所有元素的邊界相互接觸。 – 2015-04-01 16:29:22

+0

你想要邊距而不是填充 – hobberwickey 2015-04-01 16:30:56

+0

正如@hobberwickey指出的那樣,你需要一個邊緣來保證你的元素沒有填充。 – 2015-04-01 16:31:52