【问题标题】:Angular child component view not getting update角子组件视图没有得到更新
【发布时间】:2020-04-30 10:06:26
【问题描述】:

我在以下结构中有三个组件。当我尝试更新对象属性时不会反映在视图中。你能帮我看看这是什么原因吗?

app.component.html

<app-event (myNewFun)="sortList()" [myChild]=journey.course></app-event>

app.component.ts

 this.store$.pipe(select(selectObjectJourney), takeWhile(() => this.componentActive)).subscribe((result: Journey) => {
          this.journey = result;
        });
-----------
sortList() {
    this.journey.course = //doing some operation and updating the journey object but not getting reflect
  }

event.component.html

<app-child [myChildInside]="myChild"></app-child>
<button (click)="myFun()">Sort</button>

event.component.ts

@Input() myChild;
  @Output() myNewFun = new EventEmitter();
  constructor() { }

  ngOnInit() {
  }
  myFun() {
    this.myNewFun.emit();
  }

child.component.html

<ul *ngFor="let item of myChildInside">
  <li>{{item.name}} --- {{item.mark}}</li></ul>

child.component.ts

@Input() myChildInside;

【问题讨论】:

  • 你能在 stackblitz 上重现这个问题吗?

标签: angular angular9


【解决方案1】:

更改对象的值时未在组件中触发更改检测。为了触发更改检测,您可以像这样更改参考

试试这个:

this.journey = { ...this.journey, course: this.journey.course.sort(sortPredicate('title', false, (title: string) => title && title.toLowerCase()))}

【讨论】:

  • 我会试试的,然后告诉你。谢谢
  • this.journey.course = this.journey.course.sort(sortPredicate('title', false, (title: string) =&gt; title &amp;&amp; title.toLowerCase()));
  • 上面是代码,怎么做变化检测,this.journey还有很多其他属性
猜你喜欢
  • 1970-01-01
  • 2019-01-19
  • 1970-01-01
  • 1970-01-01
  • 2020-01-22
  • 1970-01-01
  • 1970-01-01
  • 2017-08-14
  • 2019-07-04
相关资源
最近更新 更多