【问题标题】:Customized Increment Arrows on Input doesn't work! How to collect the input value through buttons?输入上的自定义增量箭头不起作用!如何通过按钮收集输入值?
【发布时间】:2020-09-14 14:16:45
【问题描述】:

在我的应用程序开始时,使用输入箭头或键盘,我的数据发送没有任何问题。 为了使 UI 更友好,我决定隐藏浏览器箭头并实现一些按钮以在同一输入中添加和减去。 我的 html 中有以下两个元素。 一个是用于确定要添加到购物车的单元的输入,另一个元素是应该将该信息发送到另一个组件的按钮

<div class='child'>
  <div class='fourth'>
    <ul class='p-0 m-0'>
      <li *ngFor='let item of product.items' class='def-number'>
        <a onclick="this.parentNode.querySelector('input[type=number]').stepDown()" class="minus">-</a>
        <input type='number' placeholder='0' [(ngModel)]='item.quantity' (change)='this.updateCart(item)' min='0'>
        <a onclick="this.parentNode.querySelector('input[type=number]').stepUp()" class='plus'>+</a>
      </li>
    </ul>
  </div>
</div>
<div class='child d-flex align-items-center justify-content-center'>
  <div class='fifth'>
    <ul class='p-0 m-0'>
      <li *ngFor='let item of product.items' style='line-height: 82px'>
        <button class='cart' (click)='addItem()'><i class='icon-addcart'></i></button>
      </li>
    </ul>
  </div>
</div>

这是我的功能

  updateCart(item, idProduct) {
    console.log(item);
    this.cart.updateItem(item);
    this.eCart.next(this.cart.getCart());
  }

如果我使用键盘修改输入量,我的“updateCart()”函数将毫无问题地获取数据。 问题在于“加号”和“减号”按钮以及类按钮“购物车”,因为我不知道如何让它以相同的方式收集数据

谁能告诉我我应该做什么? 提前谢谢你

【问题讨论】:

    标签: html angular button input angular7


    【解决方案1】:

    public updateQuantity(update: number): void {
      this.item.quantity += update;
    }
    
    public updateCart(item): void {
        console.log(item);
        this.cart.updateItem(item);
        this.eCart.next(this.cart.getCart());
    }
    <div class='child'>
      <div class='fourth'>
        <ul class='p-0 m-0'>
          <li *ngFor='let item of product.items' class='def-number'>
            <a (click)='updateQuantity(-1)' class="minus">-</a>
            <input type='number' placeholder='0' [(ngModel)]='item.quantity' (ngModelChange)='updateCart(item)' min='0'>
            <a (click)='updateQuantity(1)' class='plus'>+</a>
          </li>
        </ul>
      </div>
    </div>
    <div class='child d-flex align-items-center justify-content-center'>
      <div class='fifth'>
        <ul class='p-0 m-0'>
          <li *ngFor='let item of product.items' style='line-height: 82px'>
            <button class='cart' (click)='addItem()'><i class='icon-addcart'></i></button>
          </li>
        </ul>
      </div>
    </div>

    你能试试 (ngModelChange)="updateCart($event)" 代替 (change)='updateCart(item)' 吗?

    【讨论】:

    • 感谢您的建议,但它不起作用
    • 您确定 onclick="this.parentNode.querySelector('input[type=number]').stepDown()" 正在按预期工作,默认情况下单击处理程序用作(单击) Angular 中的 =“callbackFunction()”。如果不是这种情况,我建议直接更改 ngModel 中的 value.quantity 值,而不是使用 stepUp 和 stepDown。
    • 输入值正在改变,我尝试调用按钮上的函数,但它也不起作用
    • 我已经用我推荐的更改更新了我的初始答案,你能确认它不起作用吗?
    • 我已经在按钮上进行了尝试和测试,他们不断更改输入字段中的值,但它在 ts 中没有做任何事情
    猜你喜欢
    • 2018-01-05
    • 1970-01-01
    • 2021-02-09
    • 2021-06-23
    • 1970-01-01
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多