javascript - Angular 2 - 未捕获( promise ): RangeError: Maximum call stack size exceeded

标签 javascript json typescript angular2-services

我询问您有关我的 Typescript 代码中的问题。 我正在使用 json 格式的 Google Places API。我的问题是我无法按 ID 获取信息,因为错误是:RangeError:超出最大调用堆栈大小

我看到我是否有无限循环,但我没有。

///////////////////////restaurant.services.ts/////////////////

import { Injectable } from "@angular/core";
import { Http } from "@angular/http";
import { Observable } from "rxjs/Observable";
import { Restaurant } from "../class/restaurant.class";
import "rxjs/add/operator/map";
import "rxjs/add/operator/do"; 
import "rxjs/add/observable/of";
import "rxjs/add/operator/catch";

@Injectable()

export class RestaurantServices {

    private data: any;
    private observable: Observable<any>;
    private url: string = 'app/api/restaurant.json'

    constructor(private  http:  Http){}

    getRestaurantFromAPIWithCache() {

        if (this.data) {
            return Observable.of(this.data);
        } else if (this.observable) {
            return this.observable;
        } else {
            this.observable = this.http
                .get(this.url)
                .map(res => res.json())
                .map(response => {
                    this.observable = null;
                    this.data = response;
                    return this.data;
                })
                .catch(error => {
                    let errorMsg = 'une erreur ${error.status} est survenu en tentant de joindre ${error.url}';
                    return Observable.throw(errorMsg);
                });

            return this.observable;
        }
    }

    getRestaurantById(id:any) {
        if (!this.data) {
            return undefined;
        }
        const result = this.data.filter((resto: any) => resto.id === id);
        console.log("Dans restoServices: "+result);
        if (result.length > 0) {
            return result[0]; // recupère le premier élément du tableau
        }
    }

}

餐厅.list.ts

import { Component, OnInit } from "@angular/core";
import { Restaurant } from "./class/restaurant.class";

import { RestaurantServices } from "./services/restaurant.services";

@Component({
  moduleId: module.id,
  templateUrl: "restaurant-list.component.html",
  styleUrls: ['./css/restaurant.css']
})

export class RestaurantListComponent implements OnInit {

  restos: Restaurant[];
  errorMsg: string = "";

  constructor(private _restoServices: RestaurantServices) { }

  ngOnInit() {
    //Called after the constructor, initializing input properties, and the first call to ngOnChanges.
    //Add 'implements OnInit' to the class.
    this._restoServices.getRestaurantFromAPIWithCache()
      .subscribe(res => {
        this.restos = res.results;
      });
  }
}

餐厅.list.html

<div *ngFor="let resto of restos">
<div class="col-sm-6 col-md-4">
        <div class="thumbnail">
          <div *ngFor="let images of resto.photos" >
            <img src="https://maps.googleapis.com/maps/api/place/photo?maxwidth=640&maxheight=640&photoreference={{images.photo_reference}}&key=AIzaSyA2VI_ZemIgFgbXo7sHtUms7E7NhURqhTw" alt="img">
          </div>
              <div class="caption">
                <h3>{{resto.name}}</h3>
                  <p id="cityArea">{{resto.vicinity}}</p>
                  <a [routerLink]="['/restaurant', resto.id]">
                    <button type="button" class="btn btn-info center-block" data-toggle="modal" data-target="#myModal">Details <span class="glyphicon glyphicon-info-sign"></span></button>
                  </a>
              </div>
         </div>
      </div>

餐厅.detail.ts

///////////////////////restaurant.details.ts/////////////////

import { Component, OnInit, Input } from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";

import { Restaurant } from "./class/restaurant.class";
import { RestaurantServices } from "./services/restaurant.services";

@Component({
    moduleId:module.id,
    selector:"resto-details",
    templateUrl:"restaurant-details.component.html",
    styleUrls:['./css/restaurant.css']
})

export class RestaurantDetailsComponent implements OnInit{

    @Input() rt:Restaurant;
    resto:Restaurant;
    title:string ="";

    constructor(private route:ActivatedRoute, private router:Router, private _restoServices:RestaurantServices){}

        ngOnInit() {
            //Called after the constructor, initializing input properties, and the first call to ngOnChanges.
            //Add 'implements OnInit' to the class.

            let id= +this.route.snapshot.params['id'];
            this.resto= this._restoServices.getRestaurantById(id);
            console.log("ID detailPage "+ id);
        }

        goBack(){
            this.router.navigate(['/restaurant']);
        }


}

餐厅.details.html

<resto-details [rt]="resto"></resto-details>

<br>
       <!-- Modal -->
  <div *ngIf="resto">
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h2 class="modal-title" id="myModalLabel">{{ rt.name }}</h2>
          </div>
          <div class="modal-body">
              <!-- Description du restaurant  -->
              <div>{{ rt.name }}</div>
              <div>{{ rt.vicinity | uppercase}}</div><br>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal" (click)='goBack()'>Close</button>
          </div>
        </div>
      </div>
    </div>
  </div>              
<div *ngIf="!resto">Pas de Restaurant correspondant trouvée</div>

如果你有什么想法。 感谢您的提前。

最佳答案

完成了...我刚刚替换

const result = this.data.filter((resto: any) => resto.id === id);

this.data.results.filter((resto: any) => resto.id === id);

关于javascript - Angular 2 - 未捕获( promise ): RangeError: Maximum call stack size exceeded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46953872/

相关文章:

javascript - 还需要fastclick js吗?

javascript - 当我在输入中询问时,如何让 Javascript 显示当前时间?

javascript - JSON解析错误

python - 修改NamedTemporaryFile的内容(Python 3)

javascript - typescript 和 Jquery : what is the best practice to call a class into Jquery onclick function?

typescript - 如何使用 Typescript 的引用和构建模式将 mono repo 编译为 serverless bundle?

javascript - 如何使用特定 div 使用最接近的函数

javascript - Gmap 在 js 上无法正常工作

json - 如何从字符串表示形式的变量中获取嵌套结构?

javascript - 在没有 AMD 的情况下将 TypeScript 与 jQuery 一起使用?