angular内容投影怎么实现
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,这篇文章主要讲解了"angular内容投影怎么实现",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"angular内容投影怎么实现"吧!单内容投影利用ng
千家信息网最后更新 2025年11月07日angular内容投影怎么实现
这篇文章主要讲解了"angular内容投影怎么实现",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"angular内容投影怎么实现"吧!
单内容投影
利用ng-content来实现
标题
this is content
多内容投影
利用ng-content来实现
Herder Title
Body Title
Default Title
Footer Title
this is default01this is headerthis is default02this is bodythis is default03this is default04
有条件的内容投影-ng-template, ng-container, directive 等来配合实现
单个条件的内容投影
eg: 假设现在有一个人员列表,当某个人的money大于200的时候,额外添加组件中模板定义的内容
定义一个 appChildRef 指令来配合 ng-template 获取模板
import { Directive, TemplateRef } from '@angular/core';@Directive({ selector: '[appChildRef]'})export class ChildRefDirective { constructor(public templateRef: TemplateRef) { }} app-persons - html
Name: {{ person.name }}Money: {{ person.money }}200">
app-persons - ts
import { Component, ContentChild, OnInit } from '@angular/core';import { ChildRefDirective } from '../../../../directives/child-ref.directive';@Component({ selector: 'app-persons', templateUrl: './persons.component.html', styleUrls: ['./persons.component.scss']})export class PersonsComponent implements OnInit { persons: { name: string; money: number; }[] = [ { name: '杰克', money: 120 }, { name: '李莉', money: 210 }, { name: '张三', money: 170 }, ]; @ContentChild(ChildRefDirective, { static: true }) childRef!: ChildRefDirective; constructor() { } ngOnInit(): void { }}使用
this is child ref content
效果图

多个条件内容投影
eg: 现在希望通过 persons 数据中的字段进行绑定内嵌的模板来显示
appChildRef 调整
import { Directive, Input, TemplateRef } from '@angular/core';@Directive({ selector: '[appChildRef]'})export class ChildRefDirective { // 接受定义模板名称-通过这个名称和 persons 中的render字段对应进行显示对应的模板内容 @Input() appChildRef!: string; constructor(public templateRef: TemplateRef) { }} app-persons - html
Name: {{ person.name }}Money: {{ person.money }}
app-persons - ts
import { Component, ContentChild, ContentChildren, OnInit, QueryList } from '@angular/core';import { ChildRefDirective } from '../../../../directives/child-ref.directive';@Component({ selector: 'app-form-unit', templateUrl: './form-unit.component.html', styleUrls: ['./form-unit.component.scss']})export class FormUnitComponent implements OnInit { persons: { name: string; money: number; render?: string; }[] = [ { name: '杰克', money: 120, render: 'temp1' }, { name: '李莉', money: 210, render: 'temp2' }, { name: '张三', money: 170, render: 'temp3' }, ]; // @ContentChild(ChildRefDirective, { static: true }) childRef!: ChildRefDirective; @ContentChildren(ChildRefDirective) childrenRef!: QueryList; get tempRefs() { const aObj: any = {}; this.childrenRef.forEach(template => { const key: string = template.appChildRef; aObj[key] = template; }) return aObj; } constructor() { } ngOnInit(): void { }} 使用
{{index}}-{{person.name}}: this is temp1 {{index}}-{{person.name}}: this is temp2 {{index}}-{{person.name}}: this is temp3
效果图

感谢各位的阅读,以上就是"angular内容投影怎么实现"的内容了,经过本文的学习后,相信大家对angular内容投影怎么实现这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
内容
投影
模板
条件
学习
名称
字段
效果
效果图
张三
李莉
杰克
人员
单个
多个
就是
思路
情况
指令
数据
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
服务器定期维护
向数据库插入一亿条数据
政务软件开发哪家服务好
幸福生活网络安全相伴手抄报
我要访问网络安全
公共信息网络安全监察官网
数据库pr
静安区互联网络技术服务
cs服务器冻结时间命令
乡镇网络安全周宣传宣传横幅
创业板网络安全的股有哪些
开展网络安全改造
网络技术部岗位胜任模型
管理学网络技术分析法
服务器 汇聚 交换机
怎么安装服务器
互联网平台网络安全认证
青岛社区养老软件开发
威有网络技术有限公司
对学生宣传网络安全
Java数据库重复数据过滤
软件开发组织工具
山西同丰软件开发有限公司
黎明杀机中国的服务器是哪个
威海同和软件开发
魔域单机版怎么架设服务器
丹江口互联网软件开发统计
数据库管理员50岁
小米互联网科技公司
浙江专业软件开发哪家便宜