我该如何将从发布请求中收到的数据分配给可观察对象? -IONIC3

时间:2018-11-13 05:57:23

标签: angular rest ionic3 observable apprequests

大家好,我正在构建IONIC 3应用程序,该应用程序将访问API并发出POST和GET请求。将从POST请求接收的数据订阅到Observable变量时遇到问题。我的代码目标是发送一个后期请求,并从返回的数据中显示产品列表。以下是我的代码,但是我无法像对get请求那样将数据订阅到可观察对象:

public products_detail: Observable<any>;

 this.httpClient.post("http://www.xxx.asia/app_test/detail_shop_meals.json", postData)
      this.products_detail.subscribe(data => {
          json_load = data.toString();
          this.meal_available = data;
          shop_name = document.getElementById("shop_name").innerHTML = data.shop_name;
          food_type = document.getElementById("food_type").innerHTML = data.shop_description;
          fee = document.getElementById("fee").innerHTML = "฿"+ data.price
          console.log('my data: ', data);
          console.log('look here:', data.detail_meals_popular[0].name)
          //return this.products_detail = json_load;
        }, error => {
          console.log(error);
        });

和用于显示产品的html如下:

<h4>Most Popular</h4>
      <ul>
        <li ion-item *ngFor="let x of (products_detail | async)?.detail_meals_popular" (click)="item()">
          <div class="list_circle">
              <img src="assets/imgs/item2.png" alt="food">
          </div>
          <div class="list_circle_detail">
            <h5>{{ x.name }}</h5>
            <p>{{ x.description }}</p>
            <strong>฿ {{ x.price }}</strong>
            </div>
            <div class="clear"></div>
        </li>
      </ul> 

id喜欢使用observable变量在我的html中列出产品,在此先感谢您的帮助:-)

0 个答案:

没有答案