简体   繁体   English

父视图更新时,角子组件未更新

[英]Angular child component not updating when the parent view updates

So I have some simple synchronous local data shared amongst parent views and that all functions just fine, when the local data changes all the parent views that use it update and reflect correctly. 因此,我在父视图之间共享了一些简单的同步本地数据,并且当本地数据更改使用它的所有父视图时,所有功能都可以正常更新和正确反映。

However when I began splitting my growing project into custom components to prevent code duplication and whatnot things began to get really quirky. 但是,当我开始将不断发展的项目拆分为自定义组件以防止代码重复时,事情开始变得非常古怪。 The parent still updated normally however the custom component children did not receive any notifications of a change despite the fact that the data is binded. 父级仍正常更新,但是尽管绑定了数据,但自定义组件子级仍未收到任何更改通知。 Furthermore it's only the shared data outside of the parent, any changes on the parent itself reflect on the views normally and update as it should. 此外,它只是父级外部的共享数据,父级本身的任何更改都会正常反映在视图上,并按需更新。

So as it stands now, when the shared data changes, parts of the parents change and then you have to click the page to trigger a change detection which causes most of the child components to suddenly get the word and update however theres still some that don't get the word so you have to kind of fiddle around on the parent component a bit to get all the views updated which is annoying. 因此,就目前而言,当共享数据发生更改时,部分父项也会发生更改,然后您必须单击页面以触发更改检测,这将导致大多数子组件突然听到该单词并进行更新,但是仍有一些组件不能理解这个词,所以您必须在父组件上稍作改动才能使所有视图更新,这很烦人。

I've tried Tick() and various methods in ChangeDetectorRef and I've tried placing these in various lifecycle phases but I'm at a loss. 我已经尝试过Tick()ChangeDetectorRef各种方法,并且尝试将它们放置在生命周期的各个阶段,但是我很茫然。

Here's a child component on the parent that does not get the update 这是父级上的子级组件,无法获得更新

<does-not-update [(ngModel)]='service.data.1'></does-not-update>

And here's one that does 这是一个

<input type='number' [(ngModel)]='service.data.2'/>

As stated above, a click on the page is usually enough to kick the children into gear and update. 如上所述,单击页面通常足以使孩子适应和更新。

Theres one component that's a bit more complicated and has 2 children of itself, it passes the ngModel to one child and has that child pass some different data to it's sibling. 有一个组件稍微复杂一点,它本身有两个子组件,它将ngModel传递给一个子组件,并让该子组件将一些不同的数据传递给同级组件。 So it goes like 就像

parent -> child -> grandchild
                       |
                       V
                sibling grandchild

That latter more complicated one especially doesn't work right, a click to the page updates the child and grand child but not the sibling grand child 后者较复杂,特别是行不通,单击页面更新子孙,而不更新兄弟姐妹

How do I make sense of this mess? 我如何理解这个烂摊子?

I've faced a similar problem, it's related with Angular's change detection . 我遇到了类似的问题,它与Angular的变更检测有关 In my case, I have a parent component (app) with two nested components, table and filter. 就我而言,我有一个父组件(应用程序),其中包含两个嵌套组件:表和过滤器。 As the user types in one of filter fields the component emmits a "filterChange" event. 当用户键入过滤器字段之一时,组件将发出“ filterChange”事件。 The parent component could see the changes in the filter inmediatly, but the table component couldn't see these changes until some event (eg click) ocurrs on him. 父组件可以立即看到过滤器中的更改,但是表组件只有在发生某些事件(例如单击)之前才能看到这些更改。 I've solved this calling detectChanges of ChangeDetectorRef inside the handler of filterChange in the parent component. 我已经解决了在父组件中filterChange的处理程序中调用ChangeDetectorRef的detectChanges的问题。

 update_filter($filter){
        this.requests_filter = $filter;
        this.cd.detectChanges();
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当从父级触发更改时,Angular 子组件视图不会更新 - Angular child component view is not updating when changes are triggered from parent Angular:在子组件中更新传递对象的属性会更新父组件,而不会更新其值,为什么? - Angular : Updating property of passed object in child component updates parent component, updating its value doesn't, why? 从父组件调用时,角子组件变量未更新 - Angular child component variable not updating when called from parent component 父组件中角子组件的视图 - Angular child component's view in parent component Angular 的子组件中的父变量未更新 - The Parent Variable is not updating in the child Component in Angular 子组件中的 Angular 2 ngModel 更新父组件属性 - Angular 2 ngModel in child component updates parent component property 子组件中的Angular @Input也会更新父组件中的值 - Angular @Input in child component also updates value in parent component @Input 在父组件中更改时不会在子组件中更新 - @Input not updating in child component when changed in parent Angular 在父视图中呈现子组件 - Angular renders child component in parent view Angular - 当无效表单绑定到也是一个角度组件的子表单控制器时,父表单不会更新 - Angular - Parent form not updating, when invalid form is bound to the child form controller which is also an angular component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM