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

JavaScript to achieve progress bar effect

This article example shares the specific code of ...

How to write elegant JS code

Table of contents variable Use meaningful and pro...

js learning notes: class, super and extends keywords

Table of contents Preface 1. Create objects befor...

How to choose the format when using binlog in MySQL

Table of contents 1. Three modes of binlog 1.Stat...

We're driving IE6 to extinction on our own

In fact, we wonder every day when IE6 will really...

Detailed Introduction to MySQL Innodb Index Mechanism

1. What is an index? An index is a data structure...

JavaScript code to implement a simple calculator

This article example shares the specific code of ...

js to achieve a simple lottery function

This article shares the specific code of js to im...

Detailed process of upgrading glibc dynamic library in centos 6.9

glibc is the libc library released by gnu, that i...

Specific use of Linux man command

01. Command Overview Linux provides a rich help m...

Detailed explanation of the basic use of Apache POI

Table of contents Basic Introduction Getting Star...

How to solve the problem of MySQL query character set mismatch

Find the problem I recently encountered a problem...