【问题标题】:AngularJS: Option in a Select based on saved informationsAngularJS:基于保存信息的选择中的选项
【发布时间】:2016-06-28 10:01:12
【问题描述】:

我正在开发一个应用程序,我必须处理由其品牌、型号和阶段定义的车辆(项目)。这是 Plunker 上的一个简单版本:http://plnkr.co/edit/gA9mzMde4i9hpDfeOdWe

我根据品牌列表创建了 3 个选择。每个品牌都有一个型号列表,每个型号都有一个阶段列表。

<select ng-options="brand as brand.label for brand in list track by brand.id" ng-model="itemTemp.brand" ng-change="itemTemp.model=''; itemTemp.phase='';">
  <option value="">Choose a brand</option>
</select>

当我将信息保存在我的数据库中时,我意识到我将整个模型数组保存在我的 item.brand 中,并将整个阶段数组保存在我的 item.model 中,这实际上比 Plunker 上的示例要大得多。为了在我的数据库中保存较轻的商品,我决定只保存我的品牌和型号的标签。我将物品的重量减轻了 10 倍。

这样做的问题是,稍后当我需要检索保存在我的选择中的项目的信息时,我不能。如果我将整个数组保存在 item.brand 和 item.models 中,我的选择可以检索信息并获取正确的值。

我的问题是:有没有办法只保存标签而不是整个数组来获得相同的结果?

【问题讨论】:

    标签: angularjs select options


    【解决方案1】:

    您可以通过将保存的数据与源数组进行比较来检索它。 不幸的是,它需要遍历数据并找到匹配的条目。

    我已经编辑了你的 Plunker:http://plnkr.co/edit/dC1hMD2ZL79z37kPHAzE?p=preview

    $scope.restoreItem = () => {
        $scope.itemTemp.brand = findInArray($scope.list, $scope.itemSaved.brand, 'label');
        $scope.itemTemp.model = findInArray($scope.itemTemp.brand.models, $scope.itemSaved.model, 'label');
        $scope.itemTemp.phase = findInArray($scope.itemTemp.model.phases, $scope.itemSaved.phase);
    };
    

    【讨论】:

    • 非常感谢。它完全符合我的需要。 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    相关资源
    最近更新 更多