【问题标题】:implicit context is undefined, angular 7隐式上下文未定义,角度 7
【发布时间】:2019-07-14 13:08:58
【问题描述】:

是的,所以我正在遍历一组信息,并且信息显示了我想要的方式,但是,我的控制台中出现了一些看起来很奇怪的错误:ERROR TypeError: "_v.context.$implicit is undefined"

api服务:

private extractData(res: Response) {
    let body = res;
    return body || {};
  }

  getWeather(city: string, isoCode: string): Observable<any> {
    return this.http.get(`${this.endPoint}${city},${isoCode}${this.constants.apiKey}`)
    .pipe(map(this.extractData));
  }

使用 api 服务的组件:

  theWeather:any = [];
  countryList = COUNTRIES;
  isLoading: boolean = true;
  showWeather: boolean = false;

  constructor(private apiCall:ApiService) { }


  ngOnInit() {
    this.retrieveWeather()
  };

  retrieveWeather() {
    console.log('COUNTRY LIST', this.countryList);

    this.theWeather = [];
    this.countryList.map((element, i) => {
      this.apiCall.getWeather(element.city, element.countryIso)
        .subscribe((data: {}) => {
          element.weatherInfo = data;
        });
        this.isLoading = false;
      });
      this.showWeather = true;
  };

和html文件:

<div class="country-container">
  <div *ngIf="isLoading">
    <mat-card>
      <mat-progress-spinner class="spinner-style" color="primary" mode="indeterminate"></mat-progress-spinner>
    </mat-card>
  </div>
  <div *ngIf="showWeather" class="card-container">
    <mat-card *ngFor="let c of countryList" class="card">
      <mat-card-title>Weather for: {{c.city}}, {{c.countryName}}</mat-card-title>
      <mat-card-content>The current weather is: {{c.weatherInfo.weather[0].description}}</mat-card-content>
    </mat-card>

  </div>
</div>

最后是我的控制台的图像:

感谢您的帮助!

编辑:使标题更具体地针对问题。

【问题讨论】:

  • Countries 组件中的第 9 行是哪一行?国家/地区是什么样的?
  • @DeborahK 这是 html 组件中的第 9 行:Weather for: {{c.city}}, {{c.countryName}}标题>
  • 对不起,错误在 html 中,而不是在组件中。根据错误消息,html 的第 9 行是什么?
  • @DeborahK 抱歉,我看到之后更新了回复。
  • 以下是我在谷歌上搜索错误消息时提出的一些建议:stackoverflow.com/questions/47008316/…, stackoverflow.com/questions/44944637/…

标签: javascript angular angular-components


【解决方案1】:

当 Angular 模板循环遍历一组项目,并且一个或多个数组元素未定义时,您会收到此错误。

如果您通过使用以下符号在某个位置放置一个值来创建数组,就会发生这种情况:

myArray[1] = myObject;

如果你这样做,那么 myArray[0] 将没有值,当 angular 循环通过它时,你会得到错误“_v.context.$implicit is undefined”。

使用起来更安全:

myArray.push(myObject);

如果你从数组中删除一个元素,在索引处留下一个未定义的值,你也可能会得到这个。

【讨论】:

  • 完美的解释,在我的情况下,我使用的是array.push(),但推送的项目是undefined
【解决方案2】:

根据我在列表中的第 5 个元素(它表示第 10 行索引 4(即元素 5)),它无法获取天气状况...检查错误的请求参数和/或错误的返回数据。删除临时元素或检查它,看看错误是否“移动”。也许最好的办法是在没有 http 错误的情况下检查 undefined。

【讨论】:

    猜你喜欢
    • 2018-12-03
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    • 2014-03-07
    • 2021-04-20
    • 2015-05-22
    相关资源
    最近更新 更多