将json数组中的Unique值绑定到angularjs中的下拉列表中

时间:2017-02-21 09:06:46

标签: javascript angularjs angularjs-scope

数组就是这个,唯一的名字是:DomicileName

$scope.domicile = {};
$scope.domicile.users = [{
    DomicileName: 'European',
    PortFolioName: 'nternational Select x EM ADR',
    Alias: 'ADR x EM',
    Percentage: 1.0,
    Ticker: 'ADR',
    Category: 'ADRxEM',
    SortOrder: 1
}, {
    DomicileName: 'European',
    PortFolioName: 'International Select x EM ORD',
    Alias: 'Ord x EM',
    Percentage: 4,
    Ticker: 'ORD',
    Category: 'ADRxEM',
    SortOrder: 1
}, {
    DomicileName: 'European',
    PortFolioName: 'International Select with EM ADR',
    Alias: 'ADR w EM',
    Percentage: 4,
    Ticker: 'ADR',
    Category: 'ADRwEM',
    SortOrder: ''
}, {
    DomicileName: 'Developed Market Non-European',
    PortFolioName: 'International Select x EM ADR',
    Alias: 'ADR x EM',
    Percentage: 4,
    Ticker: 'ADR',
    Category: 'ADRxEM',
    SortOrder: 1
}];

2 个答案:

答案 0 :(得分:0)



angular.module('myApp',[]).controller('MyCtrl', function ($scope) {
    $scope.domicile = {};
    $scope.domicile.users = [{
          DomicileName: 'European',
          PortFolioName: 'nternational Select x EM ADR',
          Alias: 'ADR x EM',
          Percentage: 1.0,
          Ticker: 'ADR',
          Category: 'ADRxEM',
          SortOrder: 1
      }, {
          DomicileName: 'European',
          PortFolioName: 'International Select x EM ORD',
          Alias: 'Ord x EM',
          Percentage: 4,
          Ticker: 'ORD',
          Category: 'ADRxEM',
          SortOrder: 1
      }, {
          DomicileName: 'European',
          PortFolioName: 'International Select with EM ADR',
          Alias: 'ADR w EM',
          Percentage: 4,
          Ticker: 'ADR',
          Category: 'ADRwEM',
          SortOrder: ''
      }, {
          DomicileName: 'Developed Market Non-European',
          PortFolioName: 'International Select x EM ADR',
          Alias: 'ADR x EM',
          Percentage: 4,
          Ticker: 'ADR',
          Category: 'ADRxEM',
          SortOrder: 1
      }];
      
      _init();
      
      function _init() {
      	$scope.domicile.users = _uniq($scope.domicile.users, 'DomicileName')
      }
      
      function _uniq(array, property){
       	var flags = [], output = [], l = array.length, i;
        for( i=0; i<l; i++) {
            if( flags[array[i][property]]) continue;
            flags[array[i][property]] = true;
            output.push(array[i]);
        }
        return output;
      }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="MyCtrl">
    <select>
      <option ng-repeat="user in domicile.users">{{ user.DomicileName }}</option>
    </select>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

对于我的方法,你必须得到唯一的值,你可以使用现代数组函数过滤器,项目是当前的

    $scope.uniqueDomicileUsers = $scope.domicile.users.filter(function(item, i, ar){
         return ar.indexOf(item) === i; 
    })

您还可以检查一些应该是唯一的属性,在第二个示例中,我使用的是住所名称

  $scope.uniqueDomicileUsers = $scope.domicile.users.filter(function(item, i, ar){
  return ar.indexOf(item) === i && ar[i]['DomicileName']==item['DomicileName']; 
})