Detailed explanation of Angular component projection

Detailed explanation of Angular component projection

Overview

Dynamically change the content of component templates at runtime. It is not as complicated as routing, it is just a piece of HTML without business logic.

The ngContent directive projects an arbitrary fragment of the parent component's template onto a child component.

1. Simple Example

1. Use the <ng-content> directive in the subcomponent to mark the projection point

<div class="wrapper">
  <h2>I am a child component</h2>
  <div>This div is defined in the child component</div>
  <ng-content></ng-content> 
</div>

2. In the parent component, write the HTML fragment of the projection point to be projected to the child component into the tag of the child component

<div class="wrapper">
  <h2>I am the parent component</h2>
  <div>This div is defined in the parent component</div>
  <app-child2>
    <div>This div is the parent component projected into the child component</div>
  </app-child2>
</div>

Effect:

Subcomponents plus styles:

.wrapper{
    background: lightgreen;
}

Parent component plus style:

.wrapper{
    background: cyan;
} 

2. Multiple <ng-content> projection points

Subcomponents:

<div class="wrapper">
  <h2>I am a child component</h2>
  <ng-content selector=".header"></ng-content>
  <div>This div is defined in the child component</div>
  <ng-content selecter=".footer"></ng-content> 
</div>

Parent component:

<div class="wrapper">
  <h2>I am the parent component</h2>
  <div>This div is defined in the parent component</div>
  <app-child2>
    <div class="header">This is the page header. This div is the parent component projected into the child component. The title is {{title}}</div>
    <div class="footer">This is the footer. This div is the parent component projected into the child component</div>
  </app-child2>
</div> 

The header and footer are projected into the child component, and the title is also projected.

Interpolation expressions in projected content in the parent component template can only bind properties in the parent component, although the content will be projected into the child component.

3. Insert HTML by Angular attribute binding

Add a line to the parent component template:

<div [innerHTML]="divContent"></div>

Add a divContent attribute to the parent component, and the content is a html fragment.

divContent="<div>property binding innerHTML</div>";

Effect

4. Comparison of ngContent directive and attribute binding innerHTML

[innerHTML] is a browser specific API.

The ngContent directive is platform independent. Multiple projection points can be bound.

Give priority to ngContent directives

The above is a detailed explanation of the projection of Angular components. For more information about the projection of Angular components, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • A brief discussion on hidden content in angular4 ng-content
  • A brief discussion on Angular2 ng-content directive to embed content in components
  • How to build your own Angular component library with DevUI
  • Detailed explanation of Angular data binding and its implementation
  • Detailed explanation of the three major front-end technologies of React, Angular and Vue
  • Angular performance optimization: third-party components and lazy loading technology
  • Angular framework detailed explanation of view abstract definition
  • Detailed explanation of the role of brackets in AngularJS

<<:  PHP scheduled backup MySQL and mysqldump syntax parameters detailed

>>:  The complete version of the common Linux tool vi/vim

Recommend

Why does MySQL paging become slower and slower when using limit?

Table of contents 1. Test experiment 2. Performan...

How to use the vue timeline component

This article example shares the specific implemen...

Nodejs implements intranet penetration service

Table of contents 1. Proxy in LAN 2. Intranet pen...

Discussion on horizontal and vertical centering of elements in HTML

When we design a page, we often need to center th...

JavaScript Snake Implementation Code

This article example shares the specific code of ...

How to solve the problem of MySQL query character set mismatch

Find the problem I recently encountered a problem...

Use pure CSS to create a pulsating loader effect source code

Effect Preview Press the "Click to Preview&q...

How to set up scheduled backup tasks in Linux centos

Implementation Preparation # Need to back up the ...

Solve the problem that await does not work in forEach

1. Introduction A few days ago, I encountered a p...

Summary of methods to include file contents in HTML files

In the forum, netizens often ask, can I read the ...

How to implement https with nginx and openssl

If the server data is not encrypted and authentic...

Teach you how to subcontract uniapp and mini-programs (pictures and text)

Table of contents 1. Mini Program Subcontracting ...