A brief analysis of component templates in angular

青灯夜游
Release: 2022-05-16 21:01:03
forward
2197 people have browsed it

This article will take you through the component templates inangularand briefly introduce the relevant knowledge points: data binding, property binding, event binding, two-way data binding, content projection, etc. ,I hope to be helpful!

A brief analysis of component templates in angular

Angular is aclient## built usingHTML,CSS,TypeScript#A framework for buildingsingle-pageapplications. [Related tutorial recommendations: "angular tutorial"]

Angular is a

heavyweightframework that integrates a large number ofout-of-the-boxfunction module.

Angular is designed for large-scale application development and provides a clean and loosely coupled code organization method, making the application tidy and easier to maintain.

angualr Documentation:

  • Angular: https://angular.io/

  • Angular Chinese: https:// angular.cn/

  • Angular CLI: https://cli.angular.io/

  • Angular CLI Chinese: https://angular.cn/ cli

Component template

1. Data binding

Data binding That is, the data in the component class is displayed in the component template. When the data in the component class changes, it will automatically be synchronized to the component template (data-driven DOM).

Use

interpolation expressionfor data binding in Angular, that is,{{ }}.

{{message}}

{{getInfo()}}

{{a == b ? '相等': '不等'}}

{{'Hello Angular'}}

Copy after login

2. Attribute binding

2.1 Common attributes

Attribute binding is divided into In two cases,

binds DOM object attributesandbinds HTML tag attributes.

  • Use

    [property name]to bind DOM object properties to elements.

    Copy after login

  • Use

    [attr.attribute name]Bind HTML tag attributes to elements

    
              
    Copy after login

In most cases Below, DOM object attributes and HTML tag attributes are corresponding, so the first case is used.

But some attributes

only exist in HTML tagsand do not exist in the DOM object. In this case, you need to use the second case, such as thecolspanattribute, in the DOM object Just not.

Or custom HTML attributes also need to use the second case.

2.2 class attribute

 
Copy after login

2.3 style attribute

 
Copy after login

3. Event binding

  
Copy after login
export class AppComponent { title = "test" onSave(event: Event) { // this 指向组件类的实例对象 this.title // "test" } }
Copy after login

4. Get the native DOM object

4.1 Get## in the component template #

Copy after login

4.2 Get

Use

ViewChild

decorator to get an element in the component class

<p #paragraph>home works!</p>
Copy after login
import { AfterViewInit, ElementRef, ViewChild } from "@angular/core" export class HomeComponent implements AfterViewInit { @ViewChild("paragraph") paragraph: ElementRef<HTMLParagraphElement> | undefined ngAfterViewInit() { console.log(this.paragraph?.nativeElement) } }
Copy after login
Use

ViewChildren

Get a set of elements

<ul> <li #items>a</li> <li #items>b</li> <li #items>c</li> </ul>
Copy after login
import { AfterViewInit, QueryList, ViewChildren } from "@angular/core" @Component({ selector: "app-home", templateUrl: "./home.component.html", styles: [] }) export class HomeComponent implements AfterViewInit { @ViewChildren("items") items: QueryList<HTMLLIElement> | undefined ngAfterViewInit() { console.log(this.items?.toArray()) } }
Copy after login

5. Two-way data bindingData is synchronized in both directions in the component class and component template.

Angular places the two-way data binding function in the

@angular/forms

module, so to implement two-way data binding you need to rely on this module.

import { FormsModule } from "@angular/forms" @NgModule({ imports: [FormsModule], }) export class AppModule {}
Copy after login
<input type="text" [(ngModel)]="username" /> <button (click)="change()">在组件类中更改 username</button> <div>username: {{ username }}</div>
Copy after login
export class AppComponent { username: string = "" change() { this.username = "hello Angular" } }
Copy after login

6. Content projection

  
Heading
Body
Copy after login
 
Copy after login
If there is only one ng-content, the select attribute is not required.

ng-content will be replaced by

in the browser. If you don't want this extra div, you can use ng -container replaces this div.

ng-content is usually used in projection: when the parent component needs to project data to the child component, it must specify where to project the data to the child component. At this time, you can use the ng-content tag. Making a placeholder will not produce a real DOM element, but will only copy the projected content.
  • ng-container is a special container tag that does not generate real dom elements, so adding attributes to the ng-container tag is invalid.
  •    Heading   Body  
    Copy after login

7. Data binding fault tolerance processing

// app.component.ts export class AppComponent { task = { person: { name: '张三' } } }
Copy after login
 {{ task.person.name }}  {{ task.person?.name }}
Copy after login

8. Global style

/* 第一种方式 在 styles.css 文件中 */ @import "~bootstrap/dist/css/bootstrap.css"; /* ~ 相对node_modules文件夹 */
Copy after login
 
Copy after login
// 第三种方式 在 angular.json 文件中 "styles": [ "./node_modules/bootstrap/dist/css/bootstrap.min.css", "src/styles.css" ]
Copy after login
For more programming related knowledge, please visit:

Programming Video

! !

The above is the detailed content of A brief analysis of component templates in angular. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!