2016-07-28 66 views
-1

我得到這個json對象數組在我的$ http.get響應。我想要顯示兩個json對象中的任何一個。如何訪問數組中的單個json對象?

// HTML代碼:

<pre ng-repeat="opportunity in opportunities track by $index"> 
    <span>{{opportunity | json}}</span> 
</pre> 

JSON對象數組中的:在同一陣列

[ 
     { 
     "type": "job", 
     "userId": "5791a4cd0a53c59c52fc3230", 
     "positionName": "aaaaaaaa", 
     "budget": "3", 
     "jobLocation": [ 
      "Bangalore", 
      "Pune" 
     ], 
     "minExp": "0 year", 
     "maxExp": "2 years", 
     "gradMarks": null, 
     "hscOrDiploma": null, 
     "ssc": null, 
     "jobDescription": "qq", 
     "selectionProcess": "cv", 
     "lastDate": "2016-07-30T18:30:00.000Z", 
     "noOfPositions": "9" 
     }, 

//第二JSON對象

 { 
     "type": "job", 
     "userId": "5791a4cd0a53c59c52fc3230", 
     "positionName": "zz", 
     "budget": "3", 
     "jobLocation": [ 
      "Bangalore", 
      "Chennai", 
      "Pune", 
      "Hyderabad", 
      "Mumbai", 
      "Gurgaon" 
     ], 
     "minExp": "2 years", 
     "maxExp": "5 years", 
     "gradMarks": null, 
     "hscOrDiploma": null, 
     "ssc": null, 
     "jobDescription": "ss", 
     "selectionProcess": "aa", 
     "lastDate": "2016-07-30T18:30:00.000Z", 
     "noOfPositions": "5" 
     } 
] 

以上是我在做什麼。請幫幫我。

+0

爲什麼喲你的意思是兩者中的任何一個?你有什麼標準嗎? –

+0

請遵循StackOverflow風格指南。我剛剛格式化了你的問題,你花了一秒鐘再次刷新它.http://stackoverflow.com/help/formatting –

+0

請通過上述鏈接中提到的格式指南,並相應地修改你的問題。我回滾了我最後一次編輯。 –

回答

3

訪問數組中的單個特定對象,即此處的機會。你可以這樣做:

$scope.theSpecificObject = $scope.opportunities[0]; 

這會從你得到數組中的第一個json對象。 希望它有幫助。

+0

我有jquery表格,我在其中展示所有機會,並且我想在其點擊上展示相同的機會對象 – RCN

1

不知道你的意思是我想要顯示兩個json對象中的任何一個。。考慮一下你要使用第一個對象:

<pre> 
    <span>{{opportunities[0] | json}}</span> 
</pre> 

對於第2個對象:

<pre> 
    <span>{{opportunities[1] | json}}</span> 
</pre> 
+0

我有jquery表格,我在其中展示所有機會,並且希望在其點擊上顯示相同的機會對象 – RCN

+0

Shashank,I不想編寫硬代碼。我想在機會點擊時顯示關聯的json對象。 – RCN

+0

你能詳細說明你的要求嗎? –

0

爲了達到預期的結果,請使用以下

HTML:

<div ng-app="myApp" ng-controller="myCtrl"> 
    <input type="number" ng-model="getObject"> 
    <pre ng-repeat="opportunity in opportunities track by $index"> 

             <span ng-if="$index==getObject">{{opportunity | json}}</span> 

             </pre> 
</div> 

Codepen- http://codepen.io/nagasai/pen/XKYPGN