Angular - 如何更新采用输入参数并第一次正确渲染它的子组件
P粉463418483
P粉463418483 2024-03-21 21:06:55
[HTML讨论组]

我的教育经历如下:


         
      

并且是以下组件

{{educationHeader}}

{{startDate}} - {{endDate}}

具有以下逻辑,用于显示从参数中获取的数据:

export class EducationItemComponent implements OnInit {

  @Input()
  education: Education;
  isCollapsed = false;
  isActive = false;
  startDate: string;
  endDate: string;
  educationHeader: string;
  educationDescription: string;

  constructor() { }

  ngOnInit(): void {
    console.log(this.education);
    this.startDate = this.education.startDate != '' ? formatDate(this.education.startDate, 'MMM yyyy', 'en-US')
        : formatDate(new Date(), 'MM YYYY', 'en-US') ;
    this.endDate = this.education.endDate != 'present' ? this.endDate = formatDate(this.education.endDate, 'MMM yyyy', 'en-US')
        : this.education.endDate;
    this.educationHeader = this.education.degree == undefined || this.education.description == undefined ? ''
        : this.education.degree + ' at ' + this.education.school;

    if (!this.education.description.enUS && this.education.description.nlNL) {
      this.educationDescription = this.education.description.nlNL;
    } else if (this.education.description.enUS) {
      this.educationDescription = this.education.description.enUS;
    }
}

我使用自定义事件来处理更新

@Output() updatedValue: EventEmitter = new EventEmitter();

  constructor() {}

  ngOnInit(): void {}

  fieldChanged(changes: SimpleChanges) {
    this.updatedValue.emit(changes);
  }

然后我有以下 html 用于操作数据:

Update education

但是,字段 [value]="loadedEducation.school" (updatedValue)="loadedEducation.school = $event" 中的更新数据不会与子组件绑定,因此在刷新并获取之前不会显示任何内容来自数据库的数据。

我可以尝试实现哪些可能性?

我尝试实现 ngOnChanges,但没有成功。

P粉463418483
P粉463418483

全部回复(1)
P粉658954914

当您更改列表中项目的属性时,loadedEducations 列表不会更改。尝试刷新列表(this.loadedEducations = returnedEducations)或在项目中使用状态管理

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号