


Let's talk about how to perform data binding on templates in angular10?
This article will introduce you to the data binding of angular10 templates, and take you through the three binding syntaxes, template/interpolation expressions, attribute binding, style binding, and event binding. Defining, two-way binding, built-in instructions, template reference variables, etc.
Overview of binding syntax
There are roughly three types of binding syntax (basic)
- model => view (one-way: interpolation, attribute binding, style binding)
- view => model (one-way: event binding) )
- view <=> model (two-way: ngModule)
[Related tutorial recommendation: "angular tutorial"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
Comparison between HTML attribute and DOM property (very important, strengthen understanding)
Understanding the difference between HTML attribute and DOM attribute is to understand how Angular binding The key to the job. Attributes are defined by HTML. Properties are accessed from DOM (Document Object Model) nodes
- Some HTML Attributes can be mapped 1:1 to Properties; for example, "id".
- Some HTML Attributes do not have corresponding Properties. For example, aria-* colSpan rowSpan.
- Some DOM Properties do not have corresponding Attributes. For example, textContent.
It is important to remember that HTML Attributes and DOM Properties are different, even if they have the same name. In Angular, the only purpose of HTML Attributes is to initialize the state of elements and directives.
Template binding uses Properties and events instead of Attributes.
When writing data binding, you are only dealing with the target object's DOM Properties and events.
Note:
This general rule can help you build a mental model of HTML Attributes and DOM Properties: Properties are responsible for initializing DOM properties and then finishing. Property values can change; Attribute values cannot.
There is one exception to this rule. Attributes can be changed via setAttribute(), which then reinitializes the corresponding DOM attributes.
Case 1: input
1 |
|
When the browser renders the input, it creates a corresponding DOM node whose value Property has been initialized to "Sarah ".
When the user enters Sally in the input, the value Property of the DOM element will become Sally. However, if you use input.getAttribute('value') to view the Attribute value of the HTML, you can see that the attribute remains unchanged - it returns Sarah.
The value attribute of HTML specifies the initial value; the value of DOM is that this property is the current value.
Case 2: Disabled Button
disabled Attribute is another example. The button's disabled Property defaults to false, so the button is enabled.
When you add the disabled Attribute, its mere appearance initializes the button's disabled Property to true, so the button is disabled.
1 |
|
Adding and removing disabled Attribute will disable and enable the button. However, the value of Attribute does not matter, which is why you cannot enable this button by writing stilldisabled.
To control the button's state, set the disabled Property,
1 2 |
|
Template/Interpolation Expression{{}} (Basic, Mastery)
In addition to binding variables, templates can also bind methods
You can also write some simple logic in templates, such as judgments or operations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
When using template expressions, please follow the following guidelines :
- Very simple
- Executes quickly
- No visible side effects (that is, the logic in the template cannot change the variables of the component)
Attribute binding (basic, mastery)
Binding picture
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Binding common properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
Bind custom properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Use interpolation expressions (not recommended)
Interpolation can also be used Attributes, but the common practice is to use square brackets []. It is recommended that the entire project maintain a unified style
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Style binding (belongs to attribute binding, basic, mastery)
Bind a single style
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Bind multiple classes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Bind a single style
1 2 3 4 5 6 7 8 9 10 11 |
|
Bind multiple styles
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
Binding priority
- A certain class or style binding is more Specifically, the higher its priority
- Binding always takes precedence over static properties
Event Binding (Basic, Mastery)
Basic usage
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
事件对象
$event 就是原生的事件对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
事件捕获或事件冒泡
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
输入输出属性(主要是子传父,通过自定义事件)
输入属性
子组件
1 2 3 4 5 6 7 8 9 10 |
|
父组件
1 2 3 4 5 6 7 8 9 10 |
|
输出属性
- 通过 new EventEmitter() 自定义一个事件;
- 调用 EventEmitter.emit(data) 发出事件,传入数据;
- 父指令通过监听自定义事件,并通过传入的 $event 对象接收数据。
子组件
1 2 3 4 5 6 7 8 9 10 11 12 |
|
父组件
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
在元数据中声明输入和输出属性
固然可以在 @Directive 和 @Component 元数据中声明 inputs 和 outputs,但不推荐提供别名
@Input()和@Output()可以接收一个参数,作为变量的别名,那么父组件中只能用别名绑定
子组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
父组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
输入属性一定要用中括号[]绑定?
如果绑定的值是静态的,就不需要[];为了统一风格尽量用上[]
1 2 3 4 5 6 7 8 9 10 |
|
双向绑定(基础,掌握)
先决条件
- 组件的属性绑定
- 组件的事件绑定
- 输入和输出(父子组件通信)
基本的双向绑定
子组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
父组件
1 2 3 4 5 6 7 |
|
双向绑定工作原理
为了使双向数据绑定有效,@Output() 属性的名字必须遵循 inputChange 模式,其中 input 是相应 @Input() 属性的名字。例如,如果 @Input() 属性为 size ,则 @Output() 属性必须为 sizeChange 。
上面的 sizerComponent 具有值属性 size 和事件属性 sizeChange。 size 属性是 @Input(),因此数据可以流入 sizerComponent 。 sizeChange 事件是一个 @Output() ,它允许数据从 sizerComponent 流出到父组件。
上面例子,有两个方法, dec() 用于减小字体大小, inc() 用于增大字体大小。这两种方法使用 resize() 在最小/最大值的约束内更改 size 属性的值,并发出带有新 size 值的事件。
简写形式
双向绑定语法是属性绑定和事件绑定的组合的简写形式
1 |
|
表单中的双向绑定
因为没有任何原生 HTML 元素遵循了 x 值和 xChange 事件的命名模式,所以与表单元素进行双向绑定需要使用 NgModel
基本使用
根据之前基本的双向绑定知识,[(ngModel)]语法可拆解为:
- 名为ngModel的输入属性
- 名为ngModelChange的输出属性
使用[(ngModule)]双向绑定的前提条件是在模块中引入FormsModule
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
1 2 3 |
|
在表单中的使用
表单中使用[(ngModel)],需要做下面两件事的其中之一
- 给控件加上name属性
- 将ngModelOptions.standalone设为true
1 2 3 4 |
|
注意:表单中使用双向数据绑定,知识点比较多,这里只做简单了解,后续会出专门章节探讨
内置指令
循环指令 *ngFor (非常基础,掌握)
1 2 3 4 5 6 7 8 9 10 11 |
|
条件渲染 *ngIf ngStyle ngClass ngSwitch(非常基础)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
双向数据绑定指令 [(ngModel)]
1 2 3 4 5 6 7 8 9 10 |
|
模板引用变量
基本使用
使用井号(#)声明模板引用变量,可以获取DOM 元素、指令、组件、TemplateRef 或 Web Component。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
ref
还有种写法就是ref, 下面两种写法是一样的
1 2 3 |
|
引用组件
在组件章节,介绍了获取子组件的属性和方法,有两种方法:本地变量和@viewChild()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
输入和输出
输入属性
子组件
1 2 3 4 5 6 7 8 9 10 |
|
父组件
1 2 3 4 5 6 7 8 9 10 |
|
输出属性
子组件
1 2 3 4 5 6 7 8 9 10 11 12 |
|
父组件
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
在元数据中声明输入和输出属性
固然可以在 @Directive 和 @Component 元数据中声明 inputs 和 outputs, 不推荐提供别名。
@Input()和@Output()可以接收一个参数,作为变量的别名,那么父组件中只能用别名绑定 子组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
父组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
输入属性一定要用中括号[]绑定?
如果绑定的值是静态的,就不需要[]
1 2 3 4 5 6 7 8 9 10 |
|
管道(基础,掌握)
常用的管道
1、大小写字母转换
1 2 3 |
|
2、 日期格式化(经常使用)
1 2 3 |
|
3、保留小数后面多少位 下面例子的含义是,3表示最少几位整数,后面的2-4表示最少最少2位小数,最多4位小数,不足补零,小数会四舍五入。
1 2 3 |
|
4、货币转换
1 2 3 4 5 6 |
|
5、字符串截取
1 2 3 |
|
6、json格式化(有时需要看一下数据)
1 |
|
自定义管道
1、创建管道文件
1 |
|
2、在管道文件中写自己的逻辑transform两个参数分别表示传入值和参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
注意:通过命令行生成的管道(过滤器),会自动在全局声明; 管道传入的参数是在':'冒号后面表示
更多编程相关知识,请访问:编程视频!!
The above is the detailed content of Let's talk about how to perform data binding on templates in angular10?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

Do you know Angular Universal? It can help the website provide better SEO support!

With the rapid development of the Internet, front-end development technology is also constantly improving and iterating. PHP and Angular are two technologies widely used in front-end development. PHP is a server-side scripting language that can handle tasks such as processing forms, generating dynamic pages, and managing access permissions. Angular is a JavaScript framework that can be used to develop single-page applications and build componentized web applications. This article will introduce how to use PHP and Angular for front-end development, and how to combine them

How to use monaco-editor in angular? The following article records the use of monaco-editor in angular that was used in a recent business. I hope it will be helpful to everyone!

The default display behavior for components in the Angular framework is not for block-level elements. This design choice promotes encapsulation of component styles and encourages developers to consciously define how each component is displayed. By explicitly setting the CSS property display, the display of Angular components can be fully controlled to achieve the desired layout and responsiveness.

PHP email templates: Customize and personalize your email content With the popularity and widespread use of email, traditional email templates can no longer meet people's needs for personalized and customized email content. Now we can create customized and personalized email templates by using PHP programming language. This article will show you how to use PHP to achieve this goal, and provide some specific code examples. 1. Create an email template First, we need to create a basic email template. This template can be an HTM

This article will take you through dependency injection, introduce the problems that dependency injection solves and its native writing method, and talk about Angular's dependency injection framework. I hope it will be helpful to you!

Authentication is one of the most important parts of any web application. This tutorial discusses token-based authentication systems and how they differ from traditional login systems. By the end of this tutorial, you will see a fully working demo written in Angular and Node.js. Traditional Authentication Systems Before moving on to token-based authentication systems, let’s take a look at traditional authentication systems. The user provides their username and password in the login form and clicks Login. After making the request, authenticate the user on the backend by querying the database. If the request is valid, a session is created using the user information obtained from the database, and the session information is returned in the response header so that the session ID is stored in the browser. Provides access to applications subject to
