gpt4 book ai didi

javascript - 如何在 select ng-options Angular 中组合多个对象属性

转载 作者:行者123 更新时间:2023-12-01 03:58:13 25 4
gpt4 key购买 nike

我想在我的选择选项中显示文本和数字。 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 - 如何在 select ng-options Angular 中组合多个对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42443810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com