我有国家的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/
答案 0 :(得分:0)
我认为你将ng-model设置为源数组。
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;