2014-03-12 54 views
0

我有一組嵌套JSON對象,都具有相同的鑰匙,所以我只是把第一個,並解開它的值代入(鍵,VAL)所以他們可以編輯。AngularJS:在一環多解包項目中的一個過濾器

{ 'tweedle beetle': { action: battle, 
         item: paddle, 
         color: blue }, 
    'poodle':   { action: eating, 
         item: noodles, 
         color: white }, 
    'bottle':   { action: sitting, 
         item: poodle, 
         color: transparent }, 
    'fox':   { action: watching, 
         item: battle, 
         color: red } 
} 

但並非所有的鍵都是可編輯的。所以我有一個可編輯字段的列表:

$scope.editableFields = ['item', 'color']; 

當循環播放第一條記錄時,我只想讓editableFields顯示出來。如何添加的「鑰匙」項目的過濾器的項目被過濾,因爲我拆開物品放入兩個值?我只想要任何可編輯項目的鍵的列表。也許有一個更好的方法去了解這方面比,取放物品[0]?

<dl class="dl-horizontal"> 
    <dt ng-repeat-start="(key,val) in items[0] | filter:editableFields"> 
     {{key}} 
    </dt> 
    <dd ng-repeat-end> 
     <input class="form-control input-md" type="text" value="{{val}}" /> 
    </dd> 
</dl> 

這是散裝編輯器的目的。我有一個項目清單,用新值更新,但我只需要一次向用戶顯示的選項。

+0

你不能做一個過濾器一樣,在對象上:HTTP://計算器的.com /一個/238427分之14789258 – JoseM

回答

0

我JoseM同意,你不能做到這一點,但這裏是這可能是有幫助的類似的例子:

<div ng-repeat="(key, data) in commands | orderObjectBy:'pos'">{{data.pos}} - {{key}}</div> 

$scope.commands = { 
     "xyz" : { 
      "group":"g2", 
      "pos":2 
     }, 
     "abc" : { 
      "group":"g2", 
      "pos":3 
     }, 
     "ijk" : { 
      "group":"g1", 
      "pos":1 
     } 
    }; 

.filter('orderObjectBy', function() { 
    return function(items, field, reverse) { 
    var filtered = []; 
    angular.forEach(items, function(item) { 
     filtered.push(item); 
    }); 
    filtered.sort(function (a, b) { 
     return (a[field] > b[field]); 
    }); 
    if(reverse) filtered.reverse(); 
    return filtered; 
    }; 
}); 
相關問題