Angular - 解除组件模板包装
创始人
2024-10-14 18:30:25
0

在Angular中,可以使用ng-template指令来解除组件模板的包装。下面是一个示例代码:

  1. 在组件的模板文件中,使用ng-template指令将内容包裹起来:

  
  
Component Template Content
  1. 在组件的父模板中,使用ng-container指令来引用ng-template:

  1. 在组件的.ts文件中,使用ViewChild装饰器获取ng-template指令的引用:
@ViewChild(TemplateRef) templateRef: TemplateRef;

这样,组件的模板内容就可以被解除包装,直接渲染在父模板中。

完整的示例代码如下:

组件模板文件(child.component.html):


  
  
Component Template Content

组件文件(child.component.ts):

import { Component, TemplateRef, ViewChild } from '@angular/core';

@Component({
  selector: 'app-child',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.css']
})
export class ChildComponent {
  @ViewChild(TemplateRef) templateRef: TemplateRef;
}

父模板文件(parent.component.html):

Parent Component

父组件文件(parent.component.ts):

import { Component, ViewChild } from '@angular/core';
import { ChildComponent } from './child/child.component';

@Component({
  selector: 'app-parent',
  templateUrl: './parent.component.html',
  styleUrls: ['./parent.component.css']
})
export class ParentComponent {
  @ViewChild(ChildComponent) child: ChildComponent;
}

通过以上代码,父组件将会渲染出子组件的模板内容,而不包含任何额外的包装。

相关内容

热门资讯

重大通报!wejoker辅助器... 重大通报!wejoker辅助器要钱玩吗,pokemmo脚本,普及教程(有挂辅助);大神普及一款德州a...
推荐一款!wepoker透视苹... 自定义wepoker透视苹果系统系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅...
实测揭晓!we-poker正规... 1、实测揭晓!we-poker正规吗,hhpoker德州透视,高科技教程(有挂透明)2、进入游戏-大...
记者揭秘!wepoker亲友圈... 1、记者揭秘!wepoker亲友圈有用吗,wepoker私人辅助器,存在挂教程(有挂技巧)2、进入游...
必备攻略!hhpoker德州透... 必备攻略!hhpoker德州透视,hhpkoer辅助器,可靠教程(有挂教程);1.hhpoker德州...
每日必备!pokemmo脚本辅... 每日必备!pokemmo脚本辅助下载,hhpoker怎么开透视,细节揭秘(有挂教程),支持语音通讯、...
我来向大家传授!wepoker... 我来向大家传授!wepoker透视有没有,hhpoker可以控制牌吗,揭秘教程(有挂技巧);小薇(透...
普及知识!如何下载wepoke... 普及知识!如何下载wepoker安装包,微扑克微乐辅助,揭秘攻略(有挂透明);玩家必备必赢加哟《13...
分享一款!佛手在线是不是有挂,... 分享一款!佛手在线是不是有挂,wepoker辅助器,曝光教程(有挂技巧)1、许多玩家不知道佛手在线是...
必看攻略!hhpoker破解工... 必看攻略!hhpoker破解工具,哈糖大菠萝有挂吗5个常用方法,技巧教程(有挂透明);哈糖大菠萝有挂...