我想在选择选项中同时显示文本和数字。 JSON对于文本和数字具有不同的属性,默认情况下,我需要将它们组合并显示在选择选项值中。
预期产量:
000.000.0001-芝加哥
HTML:
<!doctype html>
<html ng-app="plunker" >
<head>
<meta charset="utf-8">
<title>AngularJS Plunker</title>
<link rel="stylesheet" href="style.css">
<script>document.write("<base href=\"" + document.location + "\" />");</script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<select ng-model="model.selected" ng-options="item.ID as item.Title for item in items"></select>
<p>Selected: {{model.selected}}</p>
</body>
</html>
JS:
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.items = [
{ID: '000.000.0001', Title: 'Chicago'},
{ID: '000.000.0002', Title: 'New York'},
{ID: '000.000.0003', Title: 'Washington'}
];
});
最佳答案
您可以使用这种方法分配多个值
<select ng-model="model.selected" ng-options="item.ID as item.ID + '-' + item.Title for item in items"></select>
<p>Selected: {{model.selected}}</p>
angular.module("app",[])
.controller("ctrl",function($scope){
$scope.model = {
selected : '000.000.0001'
}
$scope.items = [
{ID: '000.000.0001', Title: 'Chicago'},
{ID: '000.000.0002', Title: 'New York'},
{ID: '000.000.0003', Title: 'Washington'}
];
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<select ng-model="model.selected" ng-options="item.ID as item.ID + '-' + item.Title for item in items"></select>
<p>Selected: {{model.selected}}</p>
</div>
关于javascript - 如何在选择ng-options angular中组合多个对象属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42443810/