【问题标题】:How do you hydrate a parent template with child data (Angular 11)你如何用子数据来水合父模板(Angular 11)
【发布时间】:2021-02-19 20:58:14
【问题描述】:

所以我有两个 Angular 组件,一个父组件和一个子组件。我想做以下事情:

  1. 在引用的父组件中定义一个 ng-template 子函数/变量
  2. 将该模板作为参数传递给 子组件,以及
  3. 让子组件显示这个 模板使用自己的实例数据。

App.Component(父)

import { Component } from "@angular/core";

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {}
<reusable-component [customTemplate]="parentTemplate"></reusable-component>

<ng-template #parentTemplate>
  <p>Current color is {{currentColor}}</p>

  <button (click)="changeColorToRed()">
    CHANGE BACKGROUND COLOR TO RED
  </button>

  <button (click)="changeColorToGreen()">
    CHANGE BACKGROUND COLOR TO GREEN
  </button>
</ng-template>

子组件(可重用组件)

import { Component, Input, OnInit, TemplateRef } from "@angular/core";

@Component({
  selector: "reusable-component",
  templateUrl: "./reusable-component.component.html",
  styleUrls: ["./reusable-component.component.css"]
})
export class ReusableComponentComponent implements OnInit {
  @Input() public customTemplate!: TemplateRef<HTMLElement>;
  currentColor = "white";

  constructor() {}

  ngOnInit() {}

  changeColorToRed() {
    const red = "#FF0000";
    document.body.style.background = red;
    this.currentColor = "red";
  }
  changeColorToGreen() {
    const green = "#00FF00";
    document.body.style.background = green;
    this.currentColor = "green";
  }
}
<ng-container [ngTemplateOutlet]="customTemplate || defaultTemplate">
</ng-container>

<ng-template #defaultTemplate>
  Hello, zuko here!
</ng-template>

如何为我的父模板提供来自该子组件的函数/实例变量?

这是Stackblitz with the whole project

【问题讨论】:

    标签: javascript html angular ng-template ngtemplateoutlet


    【解决方案1】:

    大部分情况都很好。用于传递数据...

    让我们首先开始定义要在子组件中传递的数据

    子组件 TS

    currentColor = "white";
    constructor() {}
    
    ngOnInit() {}
    
    changeColorToRed() {
      const red = "#FF0000";
      document.body.style.background = red;
      this.currentColor = "red";
    }
    changeColorToGreen() {
      const green = "#00FF00";
      document.body.style.background = green;
      this.currentColor = "green";
    }
    
    data = { currentColor: this.currentColor, changeColorToRed: this.changeColorToRed, changeColorToGreen: this.changeColorToGreen };
    

    现在,我们将包含数据的上下文传递给模板。使用*ngTemplateOutlet 而不是[ngTemplateOutlet] 来支持链接

    子组件 html

    <ng-container *ngTemplateOutlet="customTemplate || defaultTemplate; context: data">
    </ng-container>
    

    现在,我们使用let- 属性来接收父级中的参数

    父组件html

    <reusable-component [customTemplate]="parentTemplate"></reusable-component>
    
    <ng-template #parentTemplate let-currentColor="currentColor" let-changeColorToRed="changeColorToRed" let-changeColorToGreen="changeColorToGreen">
      <p>Current color is {{currentColor}}</p>
    
      <button (click)="changeColorToRed()">
        CHANGE BACKGROUND COLOR TO RED
      </button>
    
      <button (click)="changeColorToGreen()">
        CHANGE BACKGROUND COLOR TO GREEN
      </button>
    </ng-template>
    

    Stackblitz

    【讨论】:

    • 感谢您的回复!!我必须单独添加每个变量吗?我们正在处理一个大文件,一举获得组件本身及其所有成员变量会更好
    • 您可以将所有内容包装在一个对象中,并使用一个let- 属性来接收该对象
    • 我是否需要做任何额外的事情才能让 ViewChild 使用这个模板?在自定义和默认模板中,我都有一个
      ,我尝试在子组件中访问它。我使用 @ViewChild('container', { static: false }) public readonly containerRef!: ElementRef; 访问它这总是在自定义模板中返回 undefined 但在 defaultTemplate 中工作得很好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 2020-07-23
    • 2013-10-12
    • 1970-01-01
    相关资源
    最近更新 更多