首页 > 解决方案 > 带角度的链式数组

问题描述

如何将数组发送到将使用数组所有实例的函数?我是这样做的:(标题是包含许多电影标题的数组)我需要获取每个标题的信息:

    export class AppComponent {
    constructor(private _httpClient: HttpClient) {}
    results: any = [];
    title: string[] =["Jurassic World","Titanic"];
    apiUrl = 'http://www.omdbapi.com/?apikey=89089a31&t=';
    getMovies(title) {
    this._httpClient.get(this.apiUrl+ this.title)
   .subscribe((data: any) => {
    this.results = data.Search;
    console.log(this.results);
  })}
ngOnInit() {
this.getMovies(this.title);  } 
}

标签: angulartypescript

解决方案


我能够运行它并得到如下结果:

应用组件.ts

根据 OMD 上的文档,API 调用只是一个单一的搜索,它不喜欢多个搜索查询,看起来你一次只能传递一个字符串。

结果

在您上面的代码中,您也正在这样做:

.subscribe((data: any) => {
    this.results = data.Search;
    console.log(this.results);

在数据结果上调用 .Search?这会在打字稿上产生错误,因为您只能调用已知属性。

如果你想传递多个搜索,你需要遍历数组并将迭代器作为新项目传递,这可能不是最干净的方式,因为我是在运行中写的,但它会得到你想要的。

在此处输入图像描述


推荐阅读