javascript - 在 AngularJS 中使用 "ng-options"填充选项

标签 javascript json angularjs javascript-objects

我有一个下拉菜单,我需要使用 AngularJS 填充价格。不幸的是,返回的 JSON 结构并不简单,我无法更改它,因为它属于遗留系统。

以下是 JSON 示例:

[
    {"name" : "simpleObjectName", "price" : "27.00"},
    {"name" : "simpleObjectName2", "price" : ["26.99", "27.00", "28.00"]},
    {"name" : "complexObjectName1", "availability" : { "price" : ["25.59", "26.40", "27.50"], "availability" : "AVAILABLE"}},
    {"name" : "complexObjectName2", "availability" : { "price" : ["42.99", "22.00", "237.00"], "availability" : "UNAVAILABLE"}},
    {"name" : "complexObjectName3", "availability" : { "price" : ["23.99", "216.00", "21.00"], "availability" : "UNAVAILABLE"}},
    {"name" : "complexObjectName4", "availability" : { "price" : "21.00", "availability" : "AVAILABLE"}}
]

仅当可用性设置为可用时,我才需要在此下拉列表中显示下面的简单对象的价格以及复杂对象的价格。

我是 Angular 新手,不知道当你有这样的结构时是否有办法显示价格。有什么我可以用的吗?我尝试了过滤器,但开始出现一些错误,不得不恢复更改。

编辑:更新为 JSON。发现可能有多种与报价和颜色差异相关的价格。我需要显示所有可用的复杂对象和简单对象的所有价格。

最佳答案

您可以按如下方式显示 ng-repeat 中的对象:

<div ng-app="App" ng-controller="MainCtrl">
    <select ng-model="selectedItem" ng-options="c as (c.name + ' - ' + c.price + c.availability.price) for c in containers">
        <option value="">--select --</option>
    </select>
</div>

并过滤掉 Controller 中不可用的项目:

$scope.containers = [yourcontainerjson].filter(function ($c) {
        if ($c.availability === undefined || 
            $c.availability.availability == 'AVAILABLE') {
            return $c;
        }
 });

您还可以定义一个过滤器模块并将其应用到 ng-repeat 中。 Plunker .

关于javascript - 在 AngularJS 中使用 "ng-options"填充选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32523818/

相关文章:

javascript - 如何使用 jQuery 自动完成填充额外字段

php - 修复 MySQL/PHP 中的 JSON 格式

jquery - json 对象中的撇号用于添加斜杠

javascript - 平均堆栈应用: can't hide content using directive "ng-hide' "

javascript - 客户端集群 : Could someone explain what this line does?

javascript - React 组件内的构造函数

javascript - 如何增加 IE 10 中的同时 websockets 数量?

javascript - 查询 Geofire 中该区域前 X 条记录?

Angularjs typescript 迁移

javascript - 循环内的 AJAX 调用