简体   繁体   English

如何更改 Angular 中的变量类型

[英]How do I change the type of a variable in Angular

I use {{product.price.toLocaleString('en-US', {style: 'currency', currency: 'CAD'})}}我使用{{product.price.toLocaleString('en-US', {style: 'currency', currency: 'CAD'})}}

To display the price in my view.在我的视图中显示价格。 It was working when I was manualy typing the value in the array in Angular.当我在 Angular 中手动键入数组中的值时,它正在工作。

Now I am fetching the database to fill the product array.现在我正在获取数据库以填充产品数组。

The price field is of Type INT价格字段的类型为INT

The database result are fed to angular trought an ajax request and json encoded数据库结果被馈送到 angular 处理 ajax 请求和 json 编码

The price is displayed but not the $CA after it.显示价格,但不显示其后的 $CA。 I think the function is not seeing this value as an Number .我认为 function 没有将此值视为Number

This is the function a use to get all my product data.这是用于获取我所有产品数据的 function。

sendGetRequest(){
    this.http.get('http://localhost/ajax.php').subscribe((productList)=>{
       this.productList=productList;
    });
}

Then the angular view然后angular查看

<app-product data-ng-init="sendGetRequest()" (productAdded)="addProductToCart($event)" [products]="productList"> </app-product>

The actual data being diplayed显示的实际数据

<section *ngFor="let product of products" >
      <div >
        {{product.model}}
        <br>
        <div class="preView"><img src="./assets/{{product.img}}"></div>
        {{product.price.toLocaleString('en-US', {style: 'currency', currency: 'CAD'})}}<br>
        <button (click)="addProductToCart(product)">+</button><br>
      </div>
  </section>

So i'm not sure where and how i should work with the type.所以我不确定我应该在哪里以及如何使用这种类型。

If I change my function to;如果我将 function 更改为;

  sendGetRequest(){
    this.http.get('http://localhost/ajax.php').subscribe((productList)=>{
       this.productList=productList.map(product=>{
        product.price = +product.price; // cast to number
        return product;
      });
    });

} }

I'm getting a warning;我收到警告;

Property 'map' does not exist on type 'Object “对象”类型上不存在属性“地图”

But visual studio offers a quickfix so I have tryed it and now my code looks like;但是visual studio提供了一个快速修复,所以我已经尝试过了,现在我的代码看起来像;

 private _productList: any;
  public get productList(): any {
    return this._productList;
  }
  public set productList(value: any) {
    this._productList = value;
  }

  sendGetRequest(){
    this.http.get('http://localhost/ajax.php').subscribe((productList)=>{
       this.productList=productList.map(product=>{
        product.price = +product.price; // cast to number
        return product;
      });
    });
}

Now it is working but I still get an error for the map;现在它正在工作,但我仍然收到 map 的错误;

ERROR in src/app/store/store.component.ts:28:37 - error TS2339: Property 'map' does not exist on type 'Object'. src/app/store/store.component.ts:28:37 中的错误 - 错误 TS2339:“对象”类型上不存在属性“地图”。

28        this.productList=productList.map(product=>{

Here is an image of my screen这是我的屏幕图像

错误

sendGetRequest(){
    this.http.get('http://localhost/ajax.php').subscribe((productList)=>{
    this.productList=productList.map(product=>{
        product.price = +product.price; // cast to number
        return product;
      });
   });
}

you just need to cast it to number.您只需要将其转换为数字即可。 you are receiving string type price.您正在接收字符串类型的价格。

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

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