简体   繁体   English

Angular 2如何使用我在html中的组件中获得的数据

[英]Angular 2 How to use data I got in the html in component

Hi I am currently having difficulty binding data using Angular2. 嗨,我目前在使用Angular2绑定数据时遇到困难。

I have this data getting from server and I ngFor the data which includes price or certain product. 我有从服务器获取的数据,并且包含有价格或某些产品的数据。

But I also have math algorithm to add an additional fee on the product which means I need to pass the data that has been ngFor to the component so that component can use the data. 但是我也有数学算法来为产品增加额外的费用,这意味着我需要将ngFor的数据传递给组件,以便组件可以使用该数据。

How could I achieve this? 我怎样才能做到这一点?

 <div *ngFor="let r of products | keyValues">


      <div id="tutor-price"><span id="month">월 8회</span> <span id="price"> {{r.value['charge']}} </span></div>

</div>

I want to pass this 'r.value['charge']' data to component. 我想将此'r.value ['charge']'数据传递给组件。

Thank you! 谢谢!

You should create a new collection of products, one that has the additional property in your component. 您应该创建一个新的产品集合,该集合中的组件具有附加属性。 Iterate over this collection via ngFor . 通过ngFor遍历此集合。

Here is an example: 这是一个例子:

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

import Product from 'app/models';
import ProductService from 'app/services';

@Component({
  // boilerplate and ceremony
})
export class ProductsComponent {
  constructor(readonly productService: ProductService) {}

  ngOnInit() {
    this.productService.getAll().subscribe(products => {
      this.products = products.map(product => ({
        ...product,
        charge: calculateCharge(product)
      }));
    });
  }

  products: Array<Product & {charge?: number}> = [];
}

function calculateCharge(product: Product) {
  return 1; // stub
}

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

相关问题 如何使用角度为7.2的jsPDF将动态数据的组件html导出为PDF文件? - How can I use jsPDF with angular 7.2 to export component html of dynamic data into a PDF file? Angular:如何将服务中的数据使用到组件html中 - Angular: How to use data from a service into component html 在角度 5 中使用组件(HTML)中的指令数据 - Use directive data in component (HTML) in angular 5 Angular:组件获取输入数据后如何调用函数 - Angular: How to call function after the component got the Input data 如何使用Angular数据绑定来渲染组件? - How can I use Angular data binding to render component? 如何在 angular 的其他组件中使用参数数据 - how to use params data in other component in angular 在角度2的分量中使用html的数据的方法(方式)是什么? - what is the method(way) to use data of html in component of angular 2? Angular 2:如何在具有不同 html 元素 ID 的页面上使用相同的组件? - Angular 2: How can I use the same component on a page with different html element ids? Angular:如何在组件模板中使用index.html中引用的JavaScript? - Angular: how can I use JavaScript referenced in index.html in a component template? 我应该如何在Angular 4中的组件的HTML模板中使用Service中的BehaviorSubject类? - How should I use BehaviorSubject class from a Service in the HTML Template of a Component in Angular 4?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM