第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

將對(duì)象從 rest api 推送到數(shù)組 Angular

將對(duì)象從 rest api 推送到數(shù)組 Angular

紅糖糍粑 2022-01-07 13:26:01
在我的 Angular 應(yīng)用程序中,我調(diào)用了一個(gè)從 iTunes api 返回結(jié)果的 rest-api。我想要做的是,能夠有一個(gè)添加按鈕,讓我可以將對(duì)象(歌曲對(duì)象)添加到數(shù)組中,以便用戶可以創(chuàng)建自己的播放列表。我如何將對(duì)象從其余 api 推送到數(shù)組中?到目前為止,我的 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() {  }}
查看完整描述

2 回答

?
幕布斯6054654

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超7個(gè)贊

如果在組件的 .ts 文件中,您的查詢結(jié)果在變量中songs(這就是 this.data 的樣子),在組件的 .html 中,您可以


<ng-container *ngFor="let song of songs">

    <button type="button" (click)="addSongToPlaylist(song)">Add Song {{ song.name }}</button>

</ng-container>

然后回到 .ts 文件


addSongToPlaylist(song) {

    this.playlist.push(song);

}


查看完整回答
反對(duì) 回復(fù) 2022-01-07
?
HUWWW

TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊

如果我錯(cuò)了,請(qǐng)糾正我,根據(jù)我的理解,您希望將來自 rest API 的查詢結(jié)果添加到現(xiàn)有的歌曲數(shù)組中,即數(shù)據(jù)。您可以在這里使用擴(kuò)展運(yùn)算符的力量是示例。


  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;

    })

  }


查看完整回答
反對(duì) 回復(fù) 2022-01-07
  • 2 回答
  • 0 關(guān)注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)