隐式上下文未定义,角度为 7 [英] implicit context is undefined, angular 7

查看:26
本文介绍了隐式上下文未定义,角度为 7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,所以我正在迭代一组信息,这些信息以我想要的方式显示,但是,我的控制台中出现了一些令人惊讶的错误:ERROR TypeError: "_v.context.$implicit 未定义"

api 服务:

private extractData(res: Response) {让身体 = res;返回正文 ||{};}getWeather(city: string, isoCode: string): Observable

感谢您的帮助!

使标题更具体的问题.

解决方案

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

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

myArray[1] = myObject;

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

使用更安全:

myArray.push(myObject);

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

Right, so I am iterating over an array of information and the information is showing the way that I want it to, however, I am getting some amaing looking errors in my console: ERROR TypeError: "_v.context.$implicit is undefined"

api service:

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));
  }

component using api service:

  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;
  };

and the html file:

<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>

finally an image of my console:

Thank you for the help!

edit: made the title more specific to the issue.

解决方案

You get this error when the angular template is looping through an array of items, and one or more of the array elements is undefined.

This can happen if you create your array by placing a value in a position using the following notation:

myArray[1] = myObject;

If you do this, then myArray[0] will not have a value, and when angular loops through it, you'll get the error "_v.context.$implicit is undefined".

It's safer to use:

myArray.push(myObject);

You might also get this if you remove an element from an array leaving an undefined value at an index.

这篇关于隐式上下文未定义,角度为 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆