javascript - 使用 Angular 将 HTTP 数组值从服务导入到组件中

标签 javascript arrays json angular http

我目前正在尝试使用 Angular 构建一个应用程序,并且我有一个从在线源获取数据数组的服务。我想让这个数组在其他组件中可用,但我不确定如何将日期传递给它们。

目前我的服务代码如下所示:

import { Component } from '@angular/core';
import { Headers, Http, Response } from '@angular/http';
import { Injectable } from '@angular/core';

import { Bundle } from './bundle';

import 'rxjs/add/operator/map';


@Injectable()
export class OrderInfo {

  private apiUrl = 'http://dev.assured.api.appdatasite.com/catalog';
  bundlelist: any = {};
  products: any = {};
  selectedBundle: Bundle;

    constructor(private http: Http){
        this.getBundles();
        this.getProducts();
        this.getData();
    }

    getData() {
        const auth = btoa("usernamevalue:passwordvalue");
        const h = new Headers();
        h.set('Authorization', 'Basic ' + auth);
        return this.http.get(this.apiUrl, {headers: h})
            .map((res: Response) => res.json())
    }

    getBundles() {
        this.getData().subscribe(bdata => {
            this.bundlelist = bdata.data.bundles;
            console.log(this.bundlelist);
        })
    }


    getProducts() {
        this.getData().subscribe(pdata => {
            this.products = pdata.data.products;
            console.log(this.products);
        })
    }

    onSelect(bundledata: Bundle): void {
        this.selectedBundle = bundledata;
    };

}

我试图传递的数组是“bundlelist”和“products”。我正在尝试将捆绑数据传递到以下组件:

import { Component, Input, OnInit } from '@angular/core';
import { Headers, Http, Response } from '@angular/http';

import { Bundle } from './bundle';
import { Peripherals } from './peripherals';
import { OrderInfo } from './order.service';

@Component({
  selector: 'my-order',
  template: `
      <h1>Select Bundle</h1>
      <ul class="bundles">
        <li *ngFor="let bundledata of Bundle"
        [class.selected]="bundledata === selectedBundle"
          (click)="onSelect(bundledata)" >
          <h2>{{bundledata.id}}: {{bundledata.name}}</h2>
          <p>{{bundledata.description}}</p>
        </li>
      </ul>
      <bundle-detail [bundle]="this.selectedBundle"></bundle-detail>      
  `,
  providers: [OrderInfo]
})

export class OrderComponent {
    constructor(private orderInfo: OrderInfo) { }

    Bundle: BundleList[];

    getBundles(): void {
        this.orderInfo.getBundles().then(bundlelist => this.bundlelist = bundlelist);
        console.log('unga')
      }

   ngOnInit(): void {
    this.getBundles);
  }  

}

数组数据排列如下:

{
    "data": {
        "adverts": [],
        "bundles": [{
            "id": "1",
            "name": "Bronze Bundle",
            "price": {
                "installation": "99.99",
                "recurring": "23.99"
            },
            "products": ["1", "2", "3", "4", "9", "10", "15", "15"]
        }, {
            "id": "2",
            "name": "Silver Bundle",
            "price": {
                "installation": "99.99",
                "recurring": "23.99"
            },
            "products": ["1", "2", "2", "2", "2", "4", "9", "10", "15", "15"]
        }, {
            "id": "3",
            "name": "Gold Bundle",
            "price": {
                "installation": "99.99",
                "recurring": "25.99"
            },
            "products": ["1", "2", "4", "5", "9", "10", "15", "15"]
        }, {
            "id": "4",
            "name": "Build Your Own Bundle",
            "price": {
                "installation": "49.99",
                "recurring": "9.99"
            },
            "products": ["1", "10"]
        }],
        "products": [{
            "id": "1",
            "name": "Product 1",
            "price": {
                "upfront": null,
                "installation": "0.00",
                "recurring": "0.00"
            }
        },  {
            "id": "3",
            "name": "Product 3",
            "price": {
                "upfront": "132.00",
                "installation": "9.60",
                "recurring": "2.75"
            }
        }, {
            "id": "4",
            "name": "Product 4",
            "price": {
                "upfront": "60.00",
                "installation": "9.60",
                "recurring": "1.25"
            }
        }, {
            "id": "2",
            "name": "Product 2",
            "price": {
                "upfront": "60.00",
                "installation": "9.60",
                "recurring": "1.25"
            }
        },{
            "id": "5",
            "name": "Product 5",
            "price": {
                "upfront": "228.00",
                "installation": "9.60",
                "recurring": "4.75"
            }
        }, {
            "id": "6",
            "name": "Product 6",
            "price": {
                "upfront": "96.00",
                "installation": "9.60",
                "recurring": "2.00"
            }

        }]
    }
}

数组数据肯定会到达服务,可以从控制台日志中查看,但我无法让它在订单组件中显示值的无序列表。

如果有人能让我知道我的错误在哪里,那么我将非常感激。

最佳答案

假设没有语法错误(因为如果您的代码正是您上面发布的内容,那么它应该有语法错误)

this.orderInfo.getBundles().then(bundlelist => this.bundlelist = bundlelist);

您将数据存储在 this.bundlelist (您忘记定义它?)和您的模板中:

<li *ngFor="let bundledata of Bundle" ...>

您使用Bundle(已定义)

您的代码应该是

//Template
<li *ngFor="let bundledata of Bundle" ...>

//TS
Bundle: BundleList[];

//Template
<li *ngFor="let bundledata of bundlelist" ...>

//TS
bundlelist: BundleList[]; 

关于javascript - 使用 Angular 将 HTTP 数组值从服务导入到组件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47095080/

相关文章:

javascript - 为什么 prism.js 通过将语言类插入周围的 <pre> 标签来修改 DOM?

java - 手动添加 IndexOutOfBounds 异常

php - javascript 和 php 中字符串与数组的处理速度,以及数组是否可以在不进行操作的情况下传递给 php?

java - 适用于 Jackson 中任何列表的自定义解串器

javascript - 可以从函数原型(prototype)访问私有(private)构造函数范围的变量吗?

javascript - 如何在javascript中将子项添加到现有数组

javascript - 无法在模态中携带正确的行值

arrays - 如何使用 swift 获取数组中项目的特定限制?

jQuery 解析多维数组

java - 具有完整 POJO 数据绑定(bind)的 Jackson 自定义过滤器