2016-02-22 10 views
1

我有一個空數組使用$ http.get(..)我得到10 JSON對象稱爲$scope.mobData=[];如何訪問來自多個對象中的字段,其是存在於陣列

並被推到陣列成功和每個對象有7個數據,但我只需要兩個字段在每個對象中稱爲brandCode和brandName,它可以在所有10個對象中使用。但在我的UI部分我不能夠顯示我的品牌名稱和品牌代碼數據,這是我的html代碼

<table > 
      <thead> 
       <th>BrandCode</th> 
       <th>BrandName</th> 
       <th>Action</th> 
      </thead> 

      <tbody> 
      <tr ng-repeat="mobData in mobDatas"> 
       <td>{{mobData[index].brandCode}}</td> 
       <td>{{mobData[index].brandName}}</td> 
       <td> 
       <button type="button" class="button button-positive" ng-click="editMobData(mobData)">Edit</button> 
       <button type="button" class="button button-assertive" ng-click="deleteMobData(mobData)">Delete</button> 
       </td> 
      </tr> 
      </tbody> 
</table> 

當我使用{{mobData[o].brandCode}}我能夠查看數據,但沒有使用暴徒[0]我無法查看我的數據。並且我也試過這個{{mobData.brandCode}}我無法查看我的數據

+0

你能提供你的json對象代碼 – Rahul

+0

在表達式中不需要'[index]'。 – Mahmoud

+0

s沒有使用它我怎麼告訴用戶界面從哪個對象它應該打印brandCode有10個對象從哪個對象它將打印mobData.brandCode –

回答

0

mobData是對象,並且您使用索引器的方式使用它,但您可能需要直接訪問屬性。此外,如果您的收藏名稱是mobData,那麼ng-repeat將在mobData而不是mobDatas

我已經做了現場演示,其中包含字段brandCode和brandName的對象數組。我用ng-repeat中的$ scope中的數組來渲染表格行,就像你在問題中所做的那樣。

Live Demo

<tr ng-repeat="mobDataObj in mobData"> 
     <td>{{mobDataObj.brandCode}}</td> 
     <td>{{mobDataObj.brandName}}</td> 

注意,你必須設置的角度初始狀態。

app = angular.module('aaa', []); 
app.controller('ccc', function($scope) { 
$scope.mobData = [{brandCode: 'brand code 1', brandName: 'Brand Name 1'}, 
       {brandCode: 'brand code 2', brandName: 'Brand Name 2'}, 
       {brandCode: 'brand code 3', brandName: 'Brand Name 3'}]; 

}); 
+0

我試過,但沒有奏效,請檢查我的更新問題 –

+0

與嘗試我更新的答案,在ng-repeat中的收集可能是mobData not mobDatas – Adil

+0

我沒有得到的值 –

0

做到這一點的方法:

<td>{{mobData.brandCode}}</td> 
<td>{{mobData.brandName}}</td> 

你不需要$index,因爲你已經有mobData是相同mobDatas[$index]

+0

其實我試過這種方式,請檢查我的更新後的問題 –

+0

https:// plnkr。co/edit/p6zQcDbBkceP1SINtJ4B?p = preview –

0

你結合似乎是錯誤的,它應該像

{{mobData.brandCode}} 
{{mobData.brandName}} 
+0

我試過但我無法查看它我認爲我們應該提及brandCode和brandName的索引 –

+0

你可以發佈你的json嗎? – Dario

+0

我在評論中附加了我的plunknur,請查看 –

相關問題