当前位置: 首页 > news >正文

[Angular] 笔记 20:NgContent

chatgpt:

在Angular中,NgContent是用于内容投影(Content Projection)的一个重要概念。它允许你在一个组件中插入内容,并将这些内容投影到另一个组件中。

当你在一个组件中使用<ng-content></ng-content>标签时,它充当了一个插槽,可以让你在组件的模板中插入任意HTML内容。这样做的好处是,你可以在组件内部定义模板结构,并允许外部使用该组件时灵活地插入内容,而不是固定在组件内部。

举个例子,假设有一个<app-modal>组件,你希望能够在模态框中放置自定义的内容,而不是在组件内部硬编码。你可以在<app-modal>组件的模板中使用<ng-content></ng-content>标签,这样外部使用<app-modal>组件时,其中的内容就会被投影到<ng-content></ng-content>所在的位置。

@Component({selector: 'app-modal',template: `<div class="modal"><div class="modal-content"><ng-content></ng-content></div></div>`,
})
export class ModalComponent {// ...
}

然后,当你使用组件时,你可以在其标签内部放置任何内容,这些内容就会被投影到所在的位置。

<app-modal><h1>Title</h1><p>Modal content goes here...</p>
</app-modal>

这样,<h1>Title</h1><p>Modal content goes here...</p>就会被投影到组件内部模板中的位置。


Angular For Beginners - 24. NgContent

NgContent : a.k.a Content Projection

1. NgContent 单插槽内容投影 :

在这里插入图片描述

2. NgContent 多插槽内容投影:

在这里插入图片描述

3. detail 使用 NgContent

pokemon-detail.component.html:

<!-- 将会替代成具体的 html --> 
<ng-content select="h1"></ng-content><tr><td class="pokemon-td" [class.cool-bool]="detail.isCool">{{ detail.id }} : {{ detail.name }}{{ detail.isCool == true ? "is COOL" : "is NOT COOL" }}</td><button [routerLink]="['/pokemon', detail.id]">Details</button><button (click)="onRemove()">Remove Pokemon</button>
</tr>

4. list 投影到 detail

pokemon-list.component.html 增加一个 h1元素:

<table><thead><th>Name</th><th>Index</th></thead><tbody><app-pokemon-detail*ngFor="let pokemon of pokemons"[detail]="pokemon"(remove)="handleRemove($event)"><h1>============================</h1></app-pokemon-detail></tbody>
</table>

5. web 界面

在这里插入图片描述

相关文章:

  • dash 中的模式匹配回调函数Pattern-Matching Callbacks 8
  • 【Web】vulhub-httpd apache解析漏洞复现(1)
  • 005、数据类型
  • 油猴脚本开发,之如何添加html和css
  • nodejs+vue+ElementUi农产品团购销售系统zto2c
  • 速盾cdn:cdn加速原理是什么
  • 【23.12.30期--Spring篇】Spring的AOP介绍(详解)
  • 前端axios与python库requests的区别
  • chrome扩展程序开发之在目标页面运行自己的JS
  • python常见报错信息!错误和异常!附带处理方法
  • Spring Cloud - Eureka原理、注册、搭建、应用(全过程详解)
  • flask文件夹列表改进版--Bug追踪
  • 2023年新一代开发者工具 Vue ,正式开源!
  • Power BI - 5分钟学习合并文件
  • 【前端面经】即时设计
  • Apache Zeppelin在Apache Trafodion上的可视化
  • Cookie 在前端中的实践
  • CSS3 变换
  • DOM的那些事
  • JavaScript设计模式系列一:工厂模式
  • Java新版本的开发已正式进入轨道,版本号18.3
  • js对象的深浅拷贝
  • Linux链接文件
  • quasar-framework cnodejs社区
  • React-redux的原理以及使用
  • tensorflow学习笔记3——MNIST应用篇
  • Travix是如何部署应用程序到Kubernetes上的
  • UEditor初始化失败(实例已存在,但视图未渲染出来,单页化)
  • Vultr 教程目录
  • windows下如何用phpstorm同步测试服务器
  • 解决jsp引用其他项目时出现的 cannot be resolved to a type错误
  • 面试题:给你个id,去拿到name,多叉树遍历
  • 如何选择开源的机器学习框架?
  • 最简单的无缝轮播
  • Java数据解析之JSON
  • 哈罗单车融资几十亿元,蚂蚁金服与春华资本加持 ...
  • ​​​​​​​sokit v1.3抓手机应用socket数据包: Socket是传输控制层协议,WebSocket是应用层协议。
  • ​学习一下,什么是预包装食品?​
  • #!/usr/bin/python与#!/usr/bin/env python的区别
  • #LLM入门|Prompt#3.3_存储_Memory
  • #NOIP 2014# day.1 T3 飞扬的小鸟 bird
  • (python)数据结构---字典
  • (第61天)多租户架构(CDB/PDB)
  • (附源码)计算机毕业设计SSM疫情居家隔离服务系统
  • (南京观海微电子)——I3C协议介绍
  • (译) 函数式 JS #1:简介
  • (转)Linux整合apache和tomcat构建Web服务器
  • (转)可以带来幸福的一本书
  • .gitignore文件_Git:.gitignore
  • .NET Core工程编译事件$(TargetDir)变量为空引发的思考
  • .net 程序发生了一个不可捕获的异常
  • .net 获取url的方法
  • .NET/C# 将一个命令行参数字符串转换为命令行参数数组 args
  • .NET关于 跳过SSL中遇到的问题
  • [⑧ADRV902x]: Digital Pre-Distortion (DPD)学习笔记