Angular Select 2显示具有相应国家标志的国家/地区下拉列表

时间:2016-12-17 11:58:09

标签: javascript angularjs select2 angularjs-select2

我有国家的select2下拉列表。我得到了数组和数据的两个键的连接,但无法获得带有文本的country标志。 我的代码如下

Angular Js

$scope.select2Config = {
        formatResult: $scope.format,
        formatSelection: $scope.format
    };
    $scope.myForm = {};
    $scope.myForm.country = [
        {id: "IN", name: "INDIA"},
        {id: "USA", name: "USA"},
        {id: "UK", name: "United Kingdom"}
    ];

    $scope.genName = function (id, name) {
        return id + ' ' + name;
    }

HTML代码:

<select ng-model="myForm.country"
    ng-options="obj.id as genName(obj.id,obj.name) for obj in myForm.options">
<option value="">Select...</option></select>

如何在我选择的选项中获取图像和文字? 我在javascript中看到了1个选项,你可以参考它 http://jsfiddle.net/Starx/sgb4888k/2/

1 个答案:

答案 0 :(得分:0)

我认为你将ng-model设置为源数组。

&#13;
&#13;
angular.module('myApp', [])
  .controller('myCtrl', function($scope) {


    $scope.select2Config = {
      formatResult: $scope.format,
      formatSelection: $scope.format
    };
    $scope.myForm = {};
    $scope.selectedId = '';
    $scope.myForm.country = [{
      id: "IN",
      name: "INDIA"
    }, {
      id: "USA",
      name: "USA"
    }, {
      id: "UK",
      name: "United Kingdom"
    }];

    $scope.genName = function(id, name) {
      return id + ' ' + name;
    }

  })
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='myApp' data-ng-controller="myCtrl">

  <select ng-model="selectedId" ng-options="obj.id as genName(obj.id,obj.name) for obj in myForm.country">
    <option value="">Select...</option>
  </select>
  Selected {{selectedId}}

</div>
&#13;
&#13;
&#13;