Angular 2+无法读取未定义的属性“ id”

传奇人物

我有一个奇怪的错误,我找不到原因。根据route的:id,我得到了表privateLessons,接下来搜索具有正确ID的那个对象并将其显示在我的模板中。最后,我得到了想要的效果,数据可以正确显示,但是在控制台中出现两个相同的错误:

ERROR TypeError: Cannot read property 'id' of undefined
at Object.eval [as updateRenderer] (DetailedAnnouncementComponent.html:3)
at Object.debugUpdateRenderer [as updateRenderer] (core.es5.js:13094)
at checkAndUpdateView (core.es5.js:12241)
at callViewAction (core.es5.js:12601)
at execComponentViewsAction (core.es5.js:12533)
at checkAndUpdateView (core.es5.js:12242)
at callViewAction (core.es5.js:12601)
at execEmbeddedViewsAction (core.es5.js:12559)
at checkAndUpdateView (core.es5.js:12237)
at callViewAction (core.es5.js:12601)

如何解决?

detail-announcement.html

  <div class="detailed-announcement">
    {{ privateLesson.id }}
    {{ privateLesson.subject }}
    {{ privateLesson.category }}
    {{ privateLesson.price }}
    {{ privateLesson.level }}
    {{ privateLesson.voivodeship }}
    {{ privateLesson.city }}
    {{ privateLesson.place }}
    {{ privateLesson.description }}
    {{ privateLesson.author }}
  </div>

详细公告

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

import { PrivateLessonsService } from './../../../../services/private-lessons.service';

import { PrivateLesson } from './../../../../classes/private-lesson';


@Component({
  selector: 'app-detailed-announcement',
  templateUrl: './detailed-announcement.component.html',
  styleUrls: ['./detailed-announcement.component.scss']
})
export class DetailedAnnouncementComponent implements OnInit {

  privateLessons: PrivateLesson[];
  privateLesson: PrivateLesson;
  privateLessonIdFromParams: number;

  constructor(
    private _privateLessonsService: PrivateLessonsService,
    private _activatedRoute: ActivatedRoute  
  ) { }

  ngOnInit() {

    this._activatedRoute.params.subscribe(params => {
      this.privateLessonIdFromParams = params['id'];

    });

    this._privateLessonsService
      .getPrivateLessons()
      .subscribe(
        responseData => {
          this.privateLessons = responseData;
          this.privateLesson = this.getDimensionsByFind(this.privateLessons, this.privateLessonIdFromParams);

        }
      );

  }

  getDimensionsByFind(array: any[], id: number){
    return array.find(x => x.id === id);
  }

}
叔叔戴夫

您正在尝试在服务器响应之前显示数据,因为您正在对数据进行异步请求。

穿上*ngIfdiv以防止它过早显示数据。

  <div class="detailed-announcement" *ngIf="privateLesson">
    {{ privateLesson.id }}
    {{ privateLesson.subject }}
    {{ privateLesson.category }}
    {{ privateLesson.price }}
    {{ privateLesson.level }}
    {{ privateLesson.voivodeship }}
    {{ privateLesson.city }}
    {{ privateLesson.place }}
    {{ privateLesson.description }}
    {{ privateLesson.author }}
  </div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

类型错误:无法读取编辑视图模板上未定义的属性“Id” - Angular 2

Angular Material TypeError:无法读取未定义的属性“ id”

无法读取未定义的select2的属性“ id”

无法读取Angular 2中未定义的属性“错误”?

Angular 2`无法读取未定义的属性'dataService'

Angular2:无法读取未定义的属性“名称”

无法读取未定义的属性“ post”-Angular 2

Angular2无法读取未定义的属性“ push”

无法读取未定义[Angular2]的属性“ isStopped”

Angular 2 TypeError:无法读取未定义的属性“ toUpperCase”

无法读取未定义angular2的属性

Angular - Ionic 2 - 无法读取未定义的属性“订阅”

Angular 2-无法读取未定义的属性“ ...”

无法读取未定义Angular 2的属性

Angular 2-无法读取未定义的属性“ get”(http未定义)

Angular 2无法读取未定义类型的属性“ subscribe”:无法读取未定义的属性“ subscribe”

导航到带有 Angular 中 ID 的路由时,无法读取未定义的属性“id”

TypeError:无法读取未定义angular7的属性“ _id”

Angular6 ngrx:无法读取未定义的属性“ id”

TypeError:无法读取未定义的属性“ id”-Angular 8测试

错误类型错误:无法读取未定义的属性“ID”(Angular 8)

(Angular2 和 Typescript)Angular 的 DatePicker 无法读取未定义的属性“切换”

NgZone / Angular2 / Ionic2 TypeError:无法读取未定义的属性“运行”

无法读取原生脚本angular2中未定义的属性全局数组

angular2:错误:TypeError:无法读取未定义的属性“ ...”

Angular 2:错误类型错误:无法读取未定义的属性“值”

Angular2-无法读取嵌套调用中未定义的属性“ subscribe”

angular2形式-无法读取未定义的属性替换

无法读取未定义的属性“登录” - 测试间谍 - Angular2+