使用离子从输入文本计算价格

时间:2018-03-14 14:10:02

标签: javascript angular typescript ionic2

我想用价格创建一个简单的购物清单应用。

这是我的shopping.html

    <ion-header>
      <ion-navbar color="secondary">
        <ion-title align="center">
          My Shopping Tracker
        </ion-title>
        <ion-buttons end>
          <button ion-button icon-only (click)="addItem()"><ion-icon name="cart"></ion-icon></button>
        </ion-buttons>
      </ion-navbar>
    </ion-header>


    <ion-content>
      <ion-list>
        <ion-item *ngFor="let item of items" (click)="viewItem(item)">{{item.type}}</ion-item>
      </ion-list>
    </ion-content>

    <ion-content>
      <ion-list>
        <ion-item-sliding *ngFor="let item of items">
          <ion-item>
            <h2>{{item.today}}</h2>
            <p>{{item.type}}</p>
            <p>{{item.total}}</p>
          </ion-item>
          <ion-item-options side="right">
            <button ion-button (click)="delete(item)">
              <ion-icon name="trash"></ion-icon>Delete
            </button>
            <button ion-button (click)="edit(item)">
              <ion-icon name="redo"></ion-icon>Edit
            </button>
          </ion-item-options>
        </ion-item-sliding>
      </ion-list>
    </ion-content>

    <ion-footer>
      <ion-toolbar >
        <ion-title>{{total}}</ion-title>
      </ion-toolbar>
    </ion-footer>

这是addshopping.html

<ion-header>
  <ion-toolbar color="secondary">
    <ion-title>
      Add Shopping List
    </ion-title>
      <ion-buttons end>
        <button ion-button icon-only (click)="close()"><ion-icon name="close"></ion-icon></button>
      </ion-buttons>
    </ion-toolbar>
</ion-header>

<ion-content>
  <ion-list>

    <ion-item>
      <ion-label>Date:</ion-label>
     <ion-datetime displayFormat="DD-MM-YYYY HH:mm" [(ngModel)]="today"></ion-datetime>
    </ion-item>

    <ion-item>
      <ion-label floating>Type:</ion-label>
      <ion-input type="text" [(ngModel)]="type"></ion-input>
    </ion-item>

    <ion-item>
      <ion-label floating>Description:</ion-label>
      <ion-input type="text" [(ngModel)]="description"></ion-input>
    </ion-item>


    <ion-item>
      <ion-label floating>Total Amount:</ion-label>
      <ion-input type="text" [(ngModel)]="total"></ion-input>
    </ion-item>

  </ion-list>

  <button full ion-button color="secondary" (click)="saveItem()">Save</button>

</ion-content>

这是我的addlist.ts

    import { Component } from '@angular/core';
    import { NavController, ViewController } from 'ionic-angular';


    @Component({
      selector: 'page-addlist',
      templateUrl: 'addlist.html'
    })
    export class AddListPage {

      type: string;
      description: string;
      today: Date;
      total: price;
      constructor(public navCtrl: NavController, public view: ViewController) {

      }

      saveItem(){

        let newItem = {
          type: this.type,
          today: this.today,
          description: this.description,
          total: this.total,
        };

        this.view.dismiss(newItem);

      }

      close(){
        this.view.dismiss();
      }

    }

如何计算每次添加新商品的总价格?并在页脚上显示?我上传了图片。

Shopping Tracker

我是否需要从字符串更改为数字值?还是把它当成弦?

1 个答案:

答案 0 :(得分:1)

您可以使用返回总价的功能,例如

private getTotalPrice() {
    let totalPrice = 0;
    for (let item of itens) {
      totalPrice += Number.parseFloat(item.total);
    }
    return totalPrice;
  }

然后你可以在页脚中调用它

[(ngModel)]="getTotalPrice()"