简体   繁体   English

如何更新 Angular 中动态添加的子组件中的值 9

[英]How to update a value in the dynamically added child component in Angular 9

I have a parent component that dynamically adds child components我有一个动态添加子组件的父组件

@Component({
  selector: 'app-parent',
  template: `<p>Parent:</p><ng-container #dynamic></ng-container>`
})
export class ParentComponent implements OnInit {
  @ViewChild('dynamic', { read: ViewContainerRef }) vcr: ViewContainerRef;

  constructor(private fr: ComponentFactoryResolver) {
    
  }

  ngOnInit() {
    const factory = this.fr.resolveComponentFactory(HelloComponent);
    const ref = this.vcr.createComponent(factory);
    ref.instance.name = 'World';
  }

}

And the child component is as follows而子组件如下

@Component({
  selector: 'hello',
  template: `<h1>Hello {{name}}!</h1>`
})
export class HelloComponent {
  @Input() name: string;
}

When I change the value of name on click of a button (ref.instance.name='Bird') it doesn't update the child component view with the new value of name.当我单击按钮(ref.instance.name='Bird')更改名称的值时,它不会使用新的名称值更新子组件视图。 I tried ref.changeDetectorRef.detectChanges() as well but even that doesn't update the child component view.我也尝试了 ref.changeDetectorRef.detectChanges() 但即使这样也不会更新子组件视图。 I read articles online which said that changeDetectorRef.detectChanges() runs on the host view not on the component itself.我在网上阅读文章说 changeDetectorRef.detectChanges() 在主机视图上而不是在组件本身上运行。
How do I update the child component view when the value of name variable changes?当 name 变量的值发生变化时,如何更新子组件视图?

After you update inputs call ref.changeDetectorRef.detectChanges();更新输入后调用ref.changeDetectorRef.detectChanges(); to trigger change detection触发变更检测

ngOnInit() {
    const factory = this.fr.resolveComponentFactory(HelloComponent);
    const ref = this.vcr.createComponent(factory);
    ref.instance.name = 'World';
    ref.changeDetectorRef.detectChanges();  
}

View related manipulation should be done only inside or after ngAfterViewInit is called.视图相关的操作只能在调用 ngAfterViewInit 内部或之后进行。 I tried your sample and moved your code to ngAfterViewInit method and it started working.我尝试了您的示例并将您的代码移至 ngAfterViewInit 方法并开始工作。

@Component({
  selector: 'app-root',
  template: `<p>Parent:</p><ng-container #dynamic></ng-container>`,
  styleUrls: ['./app.component.less']
})
export class AppComponent implements AfterViewInit {
  title = 'dynamic-child';

  @ViewChild('dynamic', { read: ViewContainerRef }) vcr: ViewContainerRef;

  constructor(private fr: ComponentFactoryResolver) {
    
  }
  ngAfterViewInit(): void {
    const factory = this.fr.resolveComponentFactory(HelloComponent);
    const ref = this.vcr.createComponent(factory);
    ref.instance.name = 'World';
  }

}

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

相关问题 如何解析Angular 2中动态添加的组件的值? - How to resolve value of dynamically added component in Angular 2? 在 Angular 5 中,如何从父组件访问动态添加的子组件? - In Angular 5, how to get access to dynamically added child components from parent component? Angular 5更新子Component的Parent Component值 - Angular 5 Update Parent Component value from child Component 将值从模态子组件更新为父组件 angular 8 - Update value from modal child component to parent component angular 8 从子组件更新UI的角度将值反映到父组件 - Angular to update UI from the child component reflect the value to the parent component 如何通过更改Angular4中子组件中的值来更新父组件中数组的长度? - How can I update length of array in parent component from changing value in child component in Angular4? 如何在Angular 2,Ionic 2的组件中动态更新模板 - How to dynamically update template in component of Angular 2, Ionic 2 如何在 Angular 的子组件内动态创建组件? - How to dynamically create component inside child component in Angular? Angular-动态引用子组件 - Angular - reference child component dynamically 以编程方式设置动态添加的子组件的属性 - Programmatically set attribute of dynamically added child component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM