gpt4 book ai didi

javascript - AngularJS-ngOptions

转载 作者:行者123 更新时间:2023-12-02 16:14:42 25 4
gpt4 key购买 nike

我有一个使用经销商对象数组的选择。我可以显示经销商名称作为选项,但我还想显示距离。例如“经销商名称 - 12 英里”。

我的选择:

<select ng-show="dealers" ng-model="formData.dealer.dealerNumber"
ng-options="dealer.dealerNumber as dealer.name for dealer in dealers">
</select>

最佳答案

doc :

select as label for value in array

label: The result of this expression will be the label for <option> element. The expression will most likely refer to the value variable (e.g. value.propertyName).

angular.module('app', [])
.controller('ctrl', ["$scope",
function($scope) {
$scope.dealers = [{
dealerNumber: 1,
name: 'd',
distance: '123'
}, {
dealerNumber: 2,
name: 'dd',
distance: '124'
}, {
dealerNumber: 3,
name: 'ddd',
distance: '125'
}, {
dealerNumber: 4,
name: 'dddd',
distance: '126'
}, {
dealerNumber: 5,
name: 'ddddd',
distance: '127'
}, {
dealerNumber: 6,
name: 'dddddd',
distance: '128'
}


];

}
]);
<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-show="dealers" ng-model="dealerNumber" ng-options="dealer.dealerNumber as dealer.name+'-'+dealer.distance for dealer in dealers">
</select>
</div>

更新:使用过滤器

angular.module('app', [])
.controller('ctrl', ["$scope",
function($scope) {
$scope.dealers = [{
dealerNumber: 1,
name: 'd',
distance: '123.5'
}, {
dealerNumber: 2,
name: 'dd',
distance: '124.6'
}, {
dealerNumber: 3,
name: 'ddd',
distance: '125'
}, {
dealerNumber: 4,
name: 'dddd',
distance: '126'
}, {
dealerNumber: 5,
name: 'ddddd',
distance: '127'
}, {
dealerNumber: 6,
name: 'dddddd',
distance: '128'
}


];

}
]);
<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-show="dealers" ng-model="dealerNumber" ng-options="dealer.dealerNumber as dealer.name+'-'+(dealer.distance|number:0) for dealer in dealers">
</select>
</div>

关于javascript - AngularJS-ngOptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29790862/

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