Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 可观察代码永远不会执行_Angular_Typescript - Fatal编程技术网

Angular 可观察代码永远不会执行

Angular 可观察代码永远不会执行,angular,typescript,Angular,Typescript,我不知道为什么代码没有运行。 甚至console.log('1')也不会出现 ngOnInit() { this.getContacts(); this.getClients(); } getContacts(): Observable<any[]> { return this.http.get('/api/contacts') .map((res: any) => res.json()).flatMap((contacts:

我不知道为什么代码没有运行。 甚至console.log('1')也不会出现

  ngOnInit() {
    this.getContacts();
    this.getClients();
  }

  getContacts(): Observable<any[]> {
    return this.http.get('/api/contacts')
      .map((res: any) => res.json()).flatMap((contacts: any[]) => {
        if (contacts.length > 0) {
          console.log(1);
          const observables = Observable.forkJoin(
            contacts.map((contact: any) => {
              return this.http.get('/api/client/' + contact.company_name)
                .map((res: any) => {
                  let company_name: any = res.json();
                  contact.company_name = name;
                  console.log(contact.company_name);
                  return contact;
                });
            })
          );
          console.log(observables);
          return observables;
        }
        return Observable.of([]);
      }
    );
  }
localhost:4200/api/client的简单响应是:

{"_id":"59f43f1a3a2fc421c00ad8b1","name":"company2","street":"street 2","zipcode":"45678","city":"city2","__v":0}

我遵循的命令,以便
http
模块进行rest调用,您必须向其添加
subscribe

在您的情况下,这将是:

this.getContacts().subscribe((data) => {//do something with data})

为了让
http
模块进行rest调用,您必须向其添加
subscribe

在您的情况下,这将是:

this.getContacts().subscribe((data) => {//do something with data})

你从不订阅可观察的
getContacts
returns…我在问题中深入搜索。这个主题是重复的,但是第一个问题的标题真的很糟糕,你永远也找不到我的关键词。标题应该像我的一样编辑。我因为这个被禁止了…为什么标题需要编辑?现在,这是一个相同信息的路标。这一直都在框架文档中;你本可以不用问这个问题。我已经说过了,作者没有使用重要的关键词“可观测”,这一点非常重要……是的,现在你已经使用了这个词,重复的横幅将访问者链接起来。这就是标记重复项的要点。请参见,例如,您从未订阅可观察的
getContacts
返回……我在问题中进行了深入搜索。这个主题是重复的,但是第一个问题的标题真的很糟糕,你永远也找不到我的关键词。标题应该像我的一样编辑。我因为这个被禁止了…为什么标题需要编辑?现在,这是一个相同信息的路标。这一直都在框架文档中;你本可以不用问这个问题。我已经说过了,作者没有使用重要的关键词“可观测”,这一点非常重要……是的,现在你已经使用了这个词,重复的横幅将访问者链接起来。这就是标记重复项的要点。见例。