广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >angular内容投影详解
  • 372
分享到

angular内容投影详解

2024-04-02 19:04:59 372人浏览 泡泡鱼
摘要

目录单内容投影多内容投影单个条件的内容投影app-persons - htmlapp-persons - ts使用效果图多个条件内容投影appChildRef 调整app-perso

单内容投影

利用ng-content来实现


<!-- 组件 - app-content-single -->
<div>
  <h2>标题</h2>
  <!-- 投影内容显示位置 -->
  <ng-content></ng-content>
</div>
<!-- 使用 -->
<app-content-single>
  <div>this is content</div>
</app-content-single>

多内容投影

利用ng-content来实现


<!-- 组件 - app-content-more -->
<div>
  <h3>Herder Title</h3>
  <ng-content select=".header"></ng-content>
  <h3>Body Title</h3>
  <ng-content select="[body]"></ng-content>
  <h3>Default Title</h3>
  <ng-content></ng-content>
  <h3>Footer Title</h3>
  <ng-content select="footer"></ng-content>
</div>
<!-- 使用 -->
<app-content-more>
  <div>this is default01</div>
  <div class="header">this is header</div>
  <div>this is default02</div>
  <div body>this is body</div>
  <div>this is default03</div>
  <footer>this is footer</footer>
  <div>this is default04</div>
</app-content-more>

有条件的内容投影-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<any>) { }
}

app-persons - html


<div class="list-item" *ngFor="let person of persons;">
  <div>Name: {{ person.name }}</div>
  <div>Money: {{ person.money }}</div>
  <div *ngIf="person.money > 200">
    <ng-container *ngIf="childRef" [ngTemplateOutlet]="childRef.templateRef"></ng-container>
  </div>
</div>

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 { }
}

使用


<app-persons>
  <ng-template appChildRef>
    <div style="font-size: 14px; color: red;">this is child ref content</div>
  </ng-template>
</app-persons>

效果图

效果图

多个条件内容投影

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<any>) { }
}

app-persons - html


<div class="list-item" *ngFor="let person of persons;let i=index;">
  <div>Name: {{ person.name }}</div>
  <div>Money: {{ person.money }}</div>
  <!-- <div *ngIf="person.money > 200">
    <ng-container *ngIf="childRef" [ngTemplateOutlet]="childRef.templateRef"></ng-container>
  </div> -->
  <div *ngIf="person.render && tempRefs[person.render]">
    <!-- 配合 ngTemplateOutlet 指令给template传递当前person的数据 -->
    <ng-container *ngTemplateOutlet="tempRefs[person.render].templateRef; context: { $implicit: person, i: i }"></ng-container>
  </div>
</div>

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<ChildRefDirective>;
  get tempRefs() {
    const aObj: any = {};
    this.childrenRef.forEach(template => {
      const key: string = template.appChildRef;
      aObj[key] = template;
    })
    return aObj;
  }
  constructor() { }
  ngOnInit(): void { }
}

使用


<app-persons>
  <ng-template appChildRef="temp1" let-person let-index="i">
    <div style="font-size: 14px; color: red;">{{index}}-{{person.name}}: this is temp1</div>
  </ng-template>
  <ng-template appChildRef="temp2" let-person let-index="i">
    <div style="font-size: 14px; color: green;">{{index}}-{{person.name}}: this is temp2</div>
  </ng-template>
  <ng-template appChildRef="temp3" let-person let-index="i">
    <div style="font-size: 14px; color: orange;">{{index}}-{{person.name}}: this is temp3</div>
  </ng-template>
</app-persons>

效果图

效果图

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注编程网的更多内容!

--结束END--

本文标题: angular内容投影详解

本文链接: https://www.lsjlt.com/news/160588.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • angular内容投影详解
    目录单内容投影多内容投影单个条件的内容投影app-persons - htmlapp-persons - ts使用效果图多个条件内容投影appChildRef 调整app-perso...
    99+
    2022-11-12
  • angular内容投影怎么实现
    这篇文章主要讲解了“angular内容投影怎么实现”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“angular内容投影怎么实现”吧!单内容投影利用ng-content来实现<!--&n...
    99+
    2023-06-22
  • angular中怎么进行内容投影
    这篇文章主要介绍“angular中怎么进行内容投影”,在日常操作中,相信很多人在angular中怎么进行内容投影问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”angular中...
    99+
    2022-10-19
  • 详解Angular组件之投影
    目录概述一、简单例子1、子组件中使用<ng-content>指令来标记投影点2、父组件中把要投影到子组件的投影点的html片段写到子组件的标签中二、多个<ng-co...
    99+
    2022-11-12
  • angular中的内容投影有哪几种
    本篇内容介绍了“angular中的内容投影有哪几种”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!单插槽内容...
    99+
    2022-10-19
  • 如何使用Angular组件实现内容投影
    这篇文章给大家介绍如何使用Angular组件实现内容投影,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。1. 投影一块内容容器组件这样写<div>   编...
    99+
    2022-10-19
  • Angular怎么使用ng-content进行内容投影
    小编给大家分享一下Angular怎么使用ng-content进行内容投影,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧! ...
    99+
    2022-10-19
  • Angular如何利用内容投射向组件输入ngForOf模板
    这篇文章主要介绍了Angular如何利用内容投射向组件输入ngForOf模板,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。现在,我们写一个组...
    99+
    2022-10-19
  • 详解spring-data-jpa中jpql的投影查询
    投影查询,就是仅仅检索表的部分字段。而不是粗暴的 SELECT * FROM...检索出所有列数据。例如检索用户余额信息的时候,就不需要检索用户的头像,创建日期等字段。节省了...
    99+
    2022-11-12
  • 详解Spring Data JPA系列之投影(Projection)的用法
    本文介绍了Spring Data JPA系列之投影(Projection)的用法,分享给大家在JPA的查询中,有一个不方便的地方,@Query注解,如果查询直接是Select C from Customer c...
    99+
    2023-05-31
    spring data jpa
  • Kotlin 泛型边界型变及星投影使用详解
    目录1.泛型2.型变3.型变—逆变4.型变—协变5.泛型边界6.星投影1.泛型 Android项目开发过程中普遍会遇到一个问题:adapter的样式、业务逻...
    99+
    2022-12-08
    Kotlin 泛型型变星投影 Kotlin 泛型
  • Spring Data JPA 在 @Query 中使用投影的方法示例详解
    Spring Data JPA 在 @Query 中使用投影的方法 关于投影的基本使用可以参考这篇文章:https://www.baeldung.com/spring-data-jp...
    99+
    2022-11-13
  • python2利用wxpython生成投影界面工具的图文详解
    本投影界面工具的功能: 准备好.prj投影文件,将输入文件夹内的WGS84经纬度坐标shp文件,投影为平面文件,成果自动命名为prj_***并新建在输入文件夹同一路径下。 下一步目标...
    99+
    2022-11-12
  • ReactRouterV6更新内容详解
    目录ReactRouterV6变更介绍1.<Switch>重命名为<Routes>2.<Route>的新特性变更3.嵌套路由变得更简单3.1具体变...
    99+
    2022-11-12
  • html5中组织内容详解
    这篇文章主要为大家展示了“html5中组织内容详解”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“html5中组织内容详解”这篇文章吧。建立段落 HTML会忽略你...
    99+
    2022-10-19
  • win10投影到此电脑是灰色的怎么做win10投影到此电脑是灰色的怎么解决方式详细介绍
    投影功能愈来愈多用户使用,不但是由于投影功能可以让系统软件共享屏幕,并且很方便实际操作,有一些用户就很喜欢应用投影功能来开展演试。可是有些用户应用该功能的过程中发觉这一功能表明灰色不能选,因此win10投影到此电脑是灰色的怎么做呢,实际上用...
    99+
    2023-07-11
  • 详解Python类和对象内容
    目录一、什么是Python类?二、Python类中的方法和属性2.1、Python类中的方法2.2、Python类中的属性三、面向对象的概念3.1、Python类:继承3.2、Python类:多态性3.3、Pytho...
    99+
    2022-06-02
    Python Python 对象
  • kernel劫持modprobe path内容详解
    目录exp1exp2exp1 smep:smep即用户数据不可执行,当 CPU 处于 ring0 模式时,执行用户空间的代码会触发页错误,系统根据CR4寄存器的第...
    99+
    2022-11-13
  • JVM入门之JVM内存结构内容详解
    一、java代码编译执行过程 源码编译:通过Java源码编译器将Java代码编译成JVM字节码(.class文件) 类加载:通过ClassLoader及其子类来完成...
    99+
    2022-11-12
  • 详解QML 调用 C++ 中的内容
    目录先说明一下测试环境这里主要是总结一下,怎么在 QML 文件中引用 C ++ 文件里定义的内容?1. 设置类型数据2. 设置对象指针先说明一下测试环境 编译器:vs2017x64开...
    99+
    2022-11-13
    QML调用 C++ QML 调用 C++内容
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作