首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用角上的另一个按钮打印所选择并添加的碟子列表

使用角上的另一个按钮打印所选择并添加的碟子列表
EN

Stack Overflow用户
提问于 2022-03-05 19:05:17
回答 1查看 51关注 0票数 0

我们必须建立在角上的照片

到目前为止我们有什么

到目前为止我们有什么?

我们需要建立一个简单的订购系统,我们不知道如何打印选定的几个版材。我们只打印了一个板块时,选择它和按下+按钮。有没有人知道如何选择,即使是在选择新菜的时候?提前感谢您的帮助!

代码语言:javascript
复制
import { Component } from '@angular/core';
import { PLATOS } from './lista-platos';
import { Plato } from './plato';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent {
  title = 'Restaurante';

  selectedOption!: Plato;
  printedOption!: Plato;

  options = PLATOS;

  print() {
    
    this.printedOption = this.selectedOption;
    console.log('My input: ', this.selectedOption);
  }
}
代码语言:javascript
复制
import { Plato } from './plato';

export const PLATOS: Plato[] = [
  { name: 'Macarrones con chorizo', price: 8.90 },
  { name: 'Pasta al pesto', price: 10.20 },
  { name: 'Pizza', price: 7.80 },
  { name: 'Rollitos primavera', price: 5.50 },
  { name: 'Arroz tres delicias', price: 6.70 }
];
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<select [(ngModel)]="selectedOption">
  <option *ngFor="let o of options">{{ o.name }} ({{ o.price }}€)</option>
</select>
<button (click)="print()">+</button>

<!-- <div *ngFor="let o of options">
  <div *ngIf="o == printedOption">
    <p>{{ printedOption }}</p>
  </div>
</div> -->
<p>{{ printedOption }}</p>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-05 20:05:24

就像这样:https://stackblitz.com/edit/angular-ivy-gxbejh

应用组件ts:

代码语言:javascript
复制
import { Component } from '@angular/core';
import { PLATOS } from './lista-platos';
import { Plato } from './plato';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  selectedOption: any;
  options = PLATOS;
  takenPlatos: Plato[] = [];
  totalPrice: number = 0;

  addPlato(plato: Plato) {
    this.takenPlatos.push(plato);
    this.totalPrice += plato.price;
  }

  removePlato(index: number) {
    this.totalPrice -= this.takenPlatos[index].price;
    this.takenPlatos.splice(index, 1);
  }
}

app-component html:

代码语言:javascript
复制
<h2>ORDEN</h2>

Consumicion:
<select [(ngModel)]="selectedOption">
  <option *ngFor="let option of options; let i = index" [ngValue]="option">
    {{ option.name }} ({{ option.price }}€)
  </option>
</select>
<button (click)="addPlato(selectedOption)">+</button>

<ng-container *ngIf="takenPlatos.length > 0">
  <h3>Consumiciones:</h3>
  <table>
    <tr *ngFor="let plato of takenPlatos; let i = index">
      <th>{{ plato.name }}</th>
      <th>{{ plato.price }} €</th>
      <th><button (click)="removePlato(i)">-</button></th>
    </tr>
  </table>
  <table>
    <tr>
      <th>TOTAL</th>
      <th>{{ totalPrice }} €</th>
    </tr>
  </table>
</ng-container>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71365195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档