简体   繁体   English

将 object 从 rest api 推入数组 ZC31C335EF37283C451B18BA0DD317DEZ

[英]Push object from rest api into an array Angular

In my Angular application, I am calling a rest-api which brings back results from the iTunes api.在我的 Angular 应用程序中,我调用了一个 rest-api,它从 iTunes api 中返回结果。 What I want to do is, be able to have an add button which lets me add the object(Song object) into an array, so that the user can create their own playlist.我想要做的是,能够有一个添加按钮,让我可以将对象(歌曲对象)添加到数组中,以便用户可以创建自己的播放列表。 How would I push the object into an array from the rest api?我如何将 object 从 rest api 推入阵列? My code so far for the Component.ts is:到目前为止,我的 Component.ts 代码是:

import { Component, OnInit } from '@angular/core';
import { ApiService } from '../../../services/api.service';
import { FormGroup, FormControl } from '@angular/forms';
import { FormBuilder } from '@angular/forms';
import { faSearch } from '@fortawesome/free-solid-svg-icons';
import { faRedo } from '@fortawesome/free-solid-svg-icons';
import { faHeadphones} from '@fortawesome/free-solid-svg-icons';
import { faExternalLinkAlt} from '@fortawesome/free-solid-svg-icons';

@Component({
  selector: 'app-content',
  templateUrl: './content.component.html',
  styleUrls: ['./content.component.scss']
})
export class ContentComponent {

  public data = [];
  public apiData: any;
  public loading = false;
  public noData = false;
  p: number = 1;
  faSearch = faSearch;
  faRedo = faRedo;
  faHeadphones = faHeadphones;
  faExternalLinkAlt = faExternalLinkAlt;
  searchQuery : string = "";

  constructor(private service: ApiService) { }

  getAll() {
    this.service.getAll(this.searchQuery).subscribe((results) => {
      this.loading = true;
      console.log('Data is received - Result - ', results);
      this.data = results.results;
      this.loading = false;

      if (this.data.length <= 0) {
        this.noData = true;
      } else if (this.data.length >= 1) {
        this.noData = false;
      } else {
        this.noData = true;
      }
    })
  }

  refresh(): void {
    window.location.reload();
  }  

  Search(){
   this.service.getAll(this.searchQuery).subscribe((results) => {
      this.loading = true;
      console.log('Data is received - Result - ', results);
      this.data = results.results;
      this.loading = false;
    })
  }
  ngOnInit() {

  }
}

If in the component's.ts file, you have the results of your query in the variable songs (which is what this.data seems to be), in the component's.html you can如果在组件的 .ts 文件中,您的查询结果在变量songs (这似乎是 this.data )中,在组件的.html 中,您可以

<ng-container *ngFor="let song of songs">
    <button type="button" (click)="addSongToPlaylist(song)">Add Song {{ song.name }}</button>
</ng-container>

then back in the.ts file然后回到 .ts 文件

addSongToPlaylist(song) {
    this.playlist.push(song);
}

please correct me if i am wrong, as per my understanding you want to add queried result from rest API into existing song array which is data.如果我错了,请纠正我,根据我的理解,您想将 rest API 的查询结果添加到现有的歌曲数组中,即数据。 you can use power of spread operator here is example.您可以在这里使用扩展运算符的力量是示例。

  Search(){
      this.service.getAll(this.searchQuery).subscribe((results) => {
      this.loading = true;
      console.log('Data is received - Result - ', results);
      this.data = [...this.data,...results.results]; // spread operator 
      this.loading = false;
    })
  }

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

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