javascript - 你如何用子数据水合父模板(Angular 11)
问题描述
所以我有两个 Angular 组件,一个父级和一个子级。我想做以下事情:
- 在引用子函数/变量的父组件中定义一个 ng-template
- 将该模板作为参数传递给子组件,然后
- 让子组件使用自己的实例数据显示此模板。
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>
如何为我的父模板提供来自该子组件的函数/实例变量?
解决方案
大多数事情都很好。对于传递数据...
让我们首先开始定义要在子组件中传递的数据
子组件 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>
推荐阅读
- python - 当我对从网站获取源代码的脚本进行多线程处理时,会出现竞争情况吗?[PYTHON]
- c# - Castle Windsor:SelectHandler 应该只在解析期间调用?
- php - Laravel,使用数据库自动完成
- python - 如何在基于 keras 的神经网络中使用 tensorflow 一种热编码?
- python - 使用 Beautiful Soup 和 Requests 提取数据
- java - 如何在由 CursorAdapter 和 CursorLoader 填充的 ListView 中为删除/插入操作设置动画?
- java - Java 套接字“成功”连接到端口 80 上的任何 IP
- install4j - install4j:使用 OpenJDK 运行旧启动器(exe 文件)
- java - 当我的 jtextfield 有“
" 写而不是我从文件中导入的文本? - amazon-web-services - AWS CloudFront 如何遵循 S3 存储桶重定向规则?