2014-07-14 76 views
0

我有一個ASP.Net MVC4應用程序。我有下面的JSON。我需要的是能夠檢索具有通過的CS值的特定項目的INDEX。 CS值將在ViewBag中傳遞,JSON也將在下面的工廠函數的ViewBag中傳遞。使用AngularJS從JSON檢索項目

CDlg.factory("RS", function() { 
    var rs = @Html.Raw(ViewBag.jsonCR); 
    return rs; 
}); 

這是返回RS:

[ 
   { 
      "CS": "AK", 
      "CLL": "LN", 
      "CTC": [ 
         { 
            "LT": null, 
            "CTID": 227322, 
            "Hors": 8, 
            "Units": 8, 
            "Price": 8, 
            "CEE": 0 
         } 
      ] 
   }, 
  { 
      "CS": "TX", 
      "CLL": "LN", 
      "CTC": [ 
         { 
            "LT": null, 
            "CTID": 219848, 
            "Hours": 8, 
            "Units": 8, 
            "Price": 6, 
            "CEE": 0 
         } 
      ] 
   }, 
   { 
      "CS": "UT", 
      "CLL": "LN", 
      "CTC": [ 
         { 
            "LT": null, 
            "CTID": 219928, 
            "Hours": 8, 
            "Units": 8, 
            "Price": 8, 
            "CEE": 0 
         } 
      ] 
   }, 
   { 
      "CS": "VA", 
      "CLL": "LN", 
      "CTC": [ 
         { 
            "LT": null, 
            "CTID": 220041, 
            "Hours": 8, 
            "Units": 8, 
            "Price": 0, 
            "CEE": 0 
         } 
      ] 
   }, 
   { 
      "CS": "VT", 
      "CLL": "LN", 
      "CTC": [ 
         { 
            "LT": null, 
            "CTID": 220344, 
            "Hours": 8, 
            "Units": 8, 
            "Price": 12, 
            "CEE": 0 
         } 
      ] 
   } 
] 

這是範圍變量我需要填充:

$scope.selectedCS = ????? 

如果我通過在「TX,我需要它返回完整對象:

  { 
      "CS": "TX", 
      "CLL": "LN", 
      "CTC": [ 
         { 
            "LT": null, 
            "CTID": 219848, 
            "Hours": 8, 
            "Units": 8, 
            "Price": 6, 
            "CEE": 0 
         } 
      ] 
   } 
+0

你有沒有考慮使用ng-repeat和filter?請看這裏:http://jsbin.com/qahal/4/edit – sylwester

+0

它是設置一個範圍變量不會反映在用戶界面。 – MB34

回答

0

Just iter吃和檢查:

for (var i = 0; i < rs.length; i++) { 
    if (rs[i].CS == passedInValue) { 
     return rs[i]; 
    } 
}