当前位置: 首页 > 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分钟学习合并文件
  • 【前端面经】即时设计
  • $translatePartialLoader加载失败及解决方式
  • [NodeJS] 关于Buffer
  • 《Java8实战》-第四章读书笔记(引入流Stream)
  • 「译」Node.js Streams 基础
  • 【vuex入门系列02】mutation接收单个参数和多个参数
  • C++入门教程(10):for 语句
  • CentOS 7 修改主机名
  • download使用浅析
  • es6--symbol
  • Fastjson的基本使用方法大全
  • HTTP中GET与POST的区别 99%的错误认识
  • leetcode98. Validate Binary Search Tree
  • Meteor的表单提交:Form
  • MySQL Access denied for user 'root'@'localhost' 解决方法
  • MySQL的数据类型
  • Mysql优化
  • Vue--数据传输
  • 得到一个数组中任意X个元素的所有组合 即C(n,m)
  • 翻译--Thinking in React
  • 软件开发学习的5大技巧,你知道吗?
  • 通过几道题目学习二叉搜索树
  • 职业生涯 一个六年开发经验的女程序员的心声。
  • Nginx惊现漏洞 百万网站面临“拖库”风险
  • 从如何停掉 Promise 链说起
  • 整理一些计算机基础知识!
  • ​ ​Redis(五)主从复制:主从模式介绍、配置、拓扑(一主一从结构、一主多从结构、树形主从结构)、原理(复制过程、​​​​​​​数据同步psync)、总结
  • ​比特币大跌的 2 个原因
  • # 飞书APP集成平台-数字化落地
  • #includecmath
  • #Js篇:单线程模式同步任务异步任务任务队列事件循环setTimeout() setInterval()
  • $Django python中使用redis, django中使用(封装了),redis开启事务(管道)
  • (17)Hive ——MR任务的map与reduce个数由什么决定?
  • (笔试题)分解质因式
  • (二)pulsar安装在独立的docker中,python测试
  • (附源码)计算机毕业设计ssm-Java网名推荐系统
  • (简单) HDU 2612 Find a way,BFS。
  • (十二)springboot实战——SSE服务推送事件案例实现
  • .NET 中小心嵌套等待的 Task,它可能会耗尽你线程池的现有资源,出现类似死锁的情况
  • .net图片验证码生成、点击刷新及验证输入是否正确
  • [2023-年度总结]凡是过往,皆为序章