资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

angular中怎么进行内容投影

这篇文章主要介绍“angular中怎么进行内容投影”,在日常操作中,相信很多人在angular中怎么进行内容投影问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”angular中怎么进行内容投影”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

网站建设哪家好,找创新互联建站!专注于网页设计、网站建设、微信开发、重庆小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了铜官免费建站欢迎大家使用!

angular中怎么进行内容投影

一、 ng-content 进行内容投影

1.1 ng-content

ng-content 元素是一个用来插入外部或者动态内容的占位符。父组件将外部内容传递给子组件,当 Angular 解析模板时,就会在子组件模板中 ng-content 出现的地方插入外部内容。

我们可以使用内容投影来创建可重用的组件。这些组件有相似的逻辑和布局,并且可以在许多地方使用。一般我们在封装一些公共组件的时候经常会用到。

1.2 为什么使用内容投影

定义一个 button 组件:

button-component.ts

@Component({
    selector: '[appButton]',
    template: `
    
`
})
export class AppButtonComponent {}

这个 button 组件的目的是在按钮内部加一个搜索的图标,我们实际使用如下:

click

我们发现组件只会展示搜索图标, 按钮的文本不会展示,能你会想到最常使用的 @Input 装饰器,但是如果我们不只是想传文本进来,而是传一段 html 进来呢?此时就会用到 ng-content

1.3 单插槽内容投影

内容投影的最基本形式是单插槽内容投影

单插槽内容投影是指创建一个组件,我们可以在其中投影一个组件。

以 button 组件为例,创建一个单槽内容投影:

button-component.ts

@Component({
    selector: '[appButton]',
    template: `
     
`
})
export class AppButtonComponent {}

实际使用如下:

click

可以发现,现在这个 button 组件的效果是即显示了搜索图标,又显示了按钮的文本(click)。即把 中间的内容 投影 到了组件的 位置。

ng-content 元素是一个占位符,它不会创建真正的 DOM 元素。ng-content 的那些自定义属性将被忽略。

1.4 多插槽内容投影

一个组件可以具有多个插槽,每个插槽可以指定一个 CSS 选择器,该选择器会决定将哪些内容放入该插槽。该模式称为多插槽内容投影。使用此模式,我们必须指定希望投影内容出现在的位置。可以通过使用 ng-contentselect 属性来完成此任务。

  • 组件模板含有多个 ng-content 标签。

  • 为了区分投影的内容可以投影到对应 ng-content 标签,需要使用 ng-content 标签上的 select 属性作为识别。

  • select 属性支持标签名属性CSS 类 :not 伪类的任意组合。

  • 不添加 select 属性的 ng-content 标签将作为默认插槽。所有未匹配的投影内容都会投影在该 ng-content 的位置。

以 button 组件为例,创建一个多槽内容投影:

button-component.ts

@Component({
    selector: '[appButton]',
    template: `
       
`
})
export class AppButtonComponent {}

实际使用如下:


click

 me here

1.5 ngProjectAs

在某些情况下,我们需要使用 ng-container 把一些内容包裹起来传递到组件中。大多数情况是因为我们需要使用结构型指令像 ngIf 或者 ngSwitch 等。。

在下面的例子中,我们将 header 包裹在了 ng-container 中。

@Component({
    selector: 'app-card',
    template: `
		
		  
		    
		  
          
          
   ` }) export class AppCardComponent {}

使用:


  
    
      

Angular

    
  
  One framework. Mobile & desktop.   

由于 ng-container 的存在,header 部分并没有被渲染到我们想要渲染的插槽中,而是渲染到了没有提供任何 selectorng-content 中。
在这种情况下,我们可以使用 ngProjectAs 属性。
在上面的 ng-container 加上这个属性,就可以按照我们的期望来渲染了。


  
    
      

Angular

    
     One framework. Mobile & desktop.   

二、 有条件的内容投影

如果你的元件需要有条件地渲染内容或多次渲染内容,则应配置该元件以接受一个 ng-template 元素,其中包含要有条件渲染的内容。

在这种情况下,不建议使用 ng-content 元素,因为只要元件的使用者提供了内容,即使该元件从未定义 ng-content 元素或该 ng-content 元素位于 ngIf 语句的内部,该内容也总会被初始化。

使用 ng-template 元素,你可以让元件根据你想要的任何条件显式渲染内容,并可以进行多次渲染。在显式渲染 ng-template 元素之前,Angular 不会初始化该元素的内容。

2.1 ng-container

既不是一个组件,也不是一个指令,仅仅是一个特殊的tag而已。 使用 ng-container 渲染所包含的模板内容,不包含自身。

       

My name is wyl.

    

What is you name?

  
  

My name is wyl.

  

What is you name?

另外,ng 中常见错误之一的 forif 不能写在同一标签上(在一个宿主元素上只能应用一个结构型指令),利用 ng-container 标签可以在实现功能的基础上减少层级的嵌套。

2.2 ng-template

先来看下面一段代码


    

 In template, no attributes. 

    

 In ng-container, no attributes. 

浏览器输出结果是:

In ng-container, no attributes.

中的内容不会显示。当在上面的模板中添加 ngIf 指令:


   

 ngIf with a ng-template.

   

 ngIf with an ng-container.

浏览器输出结果是:

ngIf with a ng-template.
ngIf with an ng-container.

2.3 ng-template 的配合使用


     暂时搜索不到您要的数据喔


     快快开始获取吧!

2.4 ngTemplateOutlet

插入 ng-template 创建的内嵌视图。 ngTemplateOutlet 是一个结构型指令,接收一个 TemplateRef 类型的值;



  I am span in template {{title}}

*ngTemplateOutlet = "templateRefExp; content: contentExp "

使用如下:

@Component({
  selector: 'ng-template-outlet-example',
  template: `
    
    
         
         
    Hello     Hello {{name}}!     Ahoj {{person}}! ` }) class NgTemplateOutletExample {   myContext = {$implicit: 'World', localSk: 'Svet'}; }

2.5 利用 ngTemplateOutlet 进行内容投影

@Component({
    selector: 'app-card',
    template: `
		
		  
		  	
		  
		
`
})
export class AppCardComponent {

	@ContentChild('header', { static: true }) headerTemplate: TemplateRef;

	public title = 'Test';
	public otherDate = {
	 	auth: 'me',
	 	name: 'appCard'
	};
}

使用


  
    

Angular

 {{label}} (Test) and  {{otherDate | json}} ({auth: 'me', name: 'appCard'})   

到此,关于“angular中怎么进行内容投影”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


网站题目:angular中怎么进行内容投影
当前路径:http://cdkjz.cn/article/gphjcd.html
返回首页 了解更多建站资讯
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220