使用ng-options和ng-model选择ng-repeat内部

问题描述 投票:0回答:1

早上好,

希望每个人复活节快乐,

在开始之前,我想指出这是在ServiceNow Orlando版本中开发的,它使用AngularJS 1.6.10

我被困在一段棘手的代码上,我无法正确理解,我使用ng-repeat来建立硬件设备的目录,在本例中是笔记本电脑,我们添加了一个按钮,该按钮可以快速允许人们将设备添加到购物车中,我们现在想添加一个数量字段,以便可以同时添加多个设备,我开始使用它,但是AngularJS在开始时添加了一个空单元格,我需要将其添加到默认为1,我确实做到了这一点,但是无论选择什么数量,添加到购物车中的每个商品都是1,这破坏了购物车的体验。

所以这是大多数html代码,其中还包括构建硬件设备的ng-repeat,我正在使用每个人似乎都建议使用的ng-options,在Stack Overflow中阅读了很多。

问题是ng-model,因为它被设置为$ scope.items [0],所以它永远不会更新,我一直在查看getterSetter,但目前我非常烦恼,无法解决它工作。

 <div class="col-sm-6 col-md-4 funky-show-hide" ng-show="selectedCat==item.categoryBelongTo" ng-repeat="item in data.items track by $index">
    <div class="panel panel- b" >
      <a target="" ng-href="" ng-click="onClick($event, item)" class="panel-body block">
        <div class="overflow-100">
          <h4 class="m-t-none m-b-xs hrline"><span ng-if="item.content_type == 'external'"> <i ng-if="data.isDownloadable.indexOf(item.sys_id) ==-1" class="fa fa-external-link-square" aria-hidden="true" style="color: #498fcc;"></i><i ng-if="data.isDownloadable.indexOf(item.sys_id) >-1" class="fa fa-download" aria-hidden="true" style="color: #498fcc;"></i></span></h4>
          <img ng-src="" ng-if="item.picture" class="m-r-sm m-b-sm item-image pull-left" />
          <div class="text-muted item-short-desc"></div>
        </div>
      </a>
      <div class="panel-footer" >
        <span  ng-if="item.u_show_price == 'true'" class="pull-right item-price font-bold"></span> &nbsp;
        <button ng-if="item.u_show_add_to_cart_button == 'true' " name="submit" ng-disabled="submitted" ng-click="triggerAddToCart(item.sys_id, selected.label)" class="btn btn-primary">${Add to Cart}</button>
        <select ng-if="item.u_show_quantity_button == 'true'"
                name="Quantity"
                id="quantity"
                ng-disabled="submitted"
                ng-model="selected"
                class="form-control quantity-selector"
                data-toggle="tooltip"
                tooltip-top="true" 
                data-original-title="${Quantity}"
                ng-options="item as item.label for item in items track by item.id">
        </select>
      </div>
    </div>
  </div>

控制器:

$scope.items = [{
    id: 1,
    label: '1'
}, {
    id: 2,
    label: '2'
}, {
    id: 3,
    label: '3'
}];

$scope.selected = $scope.items[0];

看起来像什么:

所以我在Performance PC笔记本电脑上选择了“ 3”,但是如果您查看右边的控制台日志,则可以看到添加的值为1。

Picture to display value added to cart

真可惜,我对空单元格感到满意,但是客户端想要的,我知道我可以用一些文本更新空单元格,但是他们真的希望它默认为1。

任何人都建议,我将不胜感激,

无聊的熊猫

html angularjs angularjs-ng-repeat angularjs-ng-model angularjs-ng-options
1个回答
0
投票

用ng-show代替ng-if解决了我的问题,试图弄清楚这一点,几天就迷失了,还开发了一个具有数字增量的更简单的解决方案

这里的代码:

<input type="number" value="1" min="1" max="20" step="1" ng-model="items_increment" title="Quantity" ng-show="item.u_show_quantity_button == 'true'"/>

$scope.items_increment = 1;

希望它可以帮助另一个人。

© www.soinside.com 2019 - 2024. All rights reserved.