简体   繁体   English

Angular 4 - 如何根据数量和选项的变化计算价格

[英]Angular 4 - How to calculate price based on change in quantity and option

Requirement要求
There are a list of items in the shopping cart.购物车中有商品清单。 Each item has option, quantity, price.每个项目都有选项,数量,价格。 Need to calculate the price based on change in quantity and option of a item.需要根据商品的数量和选项的变化来计算价格。

Research I had added [(ngModel)] as below, so as to use 2 way data-binding, however, no idea why the page simply keeps on loading and than crashes without any error.研究我添加了 [(ngModel)] 如下,以便使用 2 路数据绑定,但是,不知道为什么页面只是继续加载而不是崩溃而没有任何错误。 When i remove the ngModel attribute the page loads successfully.当我删除 ngModel 属性时,页面加载成功。

<span class="price col-1 pull-right" >{{ orderType*price}}</span>
<div class="qty" style="margin:0;">
     <span class="spinner">
        <span class="sub">-</span>
        <input type="number" id="qty" name="qty" min="1" max="100" 
               value="1" class="qty-spinner" [(ngModel)]="quantity" />
        <span class="add">+</span>
     </span>
 </div>
 <div class="type">
     <label class="radio inline" *ngFor="let option of item.options; let k = index"> 
     <input id="{{'toggle-half-'+item.id+'-'+j}}" type="radio" 
            name="{{'option-toggle-'+item.id+'-'+j}}" [value]="option.value" 
            [checked]='k===0' [(ngModel)]="orderType" />
     <span>{{option.title}} </span> 
     </label>
 </div>

TypeScript Code打字稿代码

quantity:number;
orderType:number;
price:number;

constructor() {
    this.quantity = 1;
    this.price = 0;
}

Controls used使用的控件
1) Number Control input of type number for quantity. 1) Number Control输入数量的类型编号
2) Radio Control for Options [Half/Full] 2) 选项的无线电控制 [半/全]
3) span element for printing calculated price. 3) 用于打印计算价格的跨度元素。

Expected Output预期产出
Price which is currently set to 0 should get updated when we change the quantity or the option.当前设置为 0 的价格应在我们更改数量或选项时更新。
Eg.例如。 Value set for option [ Half ] = 10为选项 [ Half ] 设置的值 = 10
Eg.例如。 Value set for option [ Full ] = 20为选项设置的值 [ Full ] = 20

Please advise how can i implement this, is the above logic and implmentation correct ?请告知我如何实现这一点,上述逻辑和实现是否正确?

Thanks in advance提前致谢

The variable option is already in use in the ngFor directive. ngFor指令中已经使用了变量option See line:见行:

<label class="radio inline" *ngFor="let option of item.options; let k = index">

Use a different variable instead for the ngModel , example:ngModel使用不同的变量,例如:

<input id="{{'toggle-half-'+item.id+'-'+j}}"  
       type="radio" 
       name="{{'option-toggle-'+item.id+'-'+j}}" 
       [value]="option.value"
       [(ngModel)]="selectedOption" />

Look at the examples in the documentation ;查看文档中的示例;

EDIT编辑

Also you have you're binding the checked attribute while imposing a ngModel .此外,您在强加ngModel时绑定了checked属性。 Instead of doing that, simply init the ngModel with the value you want, in your case,而不是这样做,只需使用您想要的值初始化ngModel ,在您的情况下,

selectedOption = item.options[0].value

Edit: Reviewing the documentation sample shared in jkris' answer, value makes sense for radio inputs.编辑:查看 jkris 回答中共享的文档示例, value对无线电输入有意义。 I still suspect checked creates an update loop when combined with [(ngModel)] .我仍然怀疑checked[(ngModel)]结合使用时会创建更新循环。


I notice you're binding checked in addition to the NgModel binding.我注意到除了 NgModel 绑定之外,您还进行了绑定checked If you remove it, does it keep the correct behavior and prevent crashing?如果删除它,它是否会保持正确的行为并防止崩溃?

<input type="number" class="qty-spinner" min="1" max="100" 
       name="quantity" [(ngModel)]="quantity" />


<input type="radio" 
       [value]="option.value" name="option" [(ngModel)]="option" />

Otherwise, another option is to use [checked] and (click) (or similar) bindings in place of NgModel.否则,另一种选择是使用[checked](click) (或类似的)绑定代替 NgModel。

Not sure why it crashed but if i were you.不知道为什么它崩溃了,但如果我是你。 The constructor构造函数

constructor() {
    this.quantity = 1;
    this.price = 0;
}

I will use ngInit instead since its a two way binding.我将改用 ngInit,因为它是一种双向绑定。

     import { OnInit } from '@angular/core';

        export class myClas implements OnInit {

        ngOnInit() {
            this.quantity = 1;
            this.price = 0;
        }

    }

Secondly, you have value in your radio button and input type number.其次,您的单选按钮和输入类型编号具有价值。 Are you sure they return value of type number ?你确定他们返回 number 类型的值吗? If i were you i will use or try string first and cast it to my desired number type afterward.如果我是你,我会先使用或尝试字符串,然后将其转换为我想要的数字类型。 Or use a pipe.或者使用管道。 Wrong type conversion can be expensive sometimes .错误的类型转换有时代价高昂。 So can you try那你可以试试吗

quantity:string;
orderType:string;
price:string;

first and do your number conversion afterward ?首先,然后做你的号码转换?

the issue is resolved now.问题现已解决。 The code snippet in the description was implemented between a div tag which had a *ngFor directive, that iterated through the list of items.描述中的代码片段是在具有 *ngFor 指令的 div 标签之间实现的,该指令遍历项目列表。

Issue问题
For dividing the list of items into 2 columns "div.col-md-6", i had implemented aa function that returned me an array of 2 arrays each containing set of items, so as to iterate through each array and display the items in each column.为了将项目列表分成 2 列“div.col-md-6”,我实现了一个函数,该函数返回一个包含 2 个数组的数组,每个数组包含一组项目,以便遍历每个数组并在其中显示项目每列。

Could not find the issue, why there was an issue only when i implemented the ngModel tag, which should have not been the case.找不到问题,为什么只有在我实现了 ngModel 标签时才会出现问题,但本不该如此。 However, i removed that splitting function and everything worked fine for me.但是,我删除了该拆分功能,一切对我来说都很好。

Thanks to all for your efforts and help.感谢大家的努力和帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM