Let's talk about how to perform data binding on templates in angular10?
青灯夜游
Release: 2021-08-03 10:31:57
forward
2041 people have browsed it
This article will introduce you to the data binding ofangular10 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)
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
Copy after login
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 useinput.getAttribute('value')to view the Attribute value of the HTML, you can see that theattribute 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.
Test Button
Copy after login
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,
import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: ` 一行文字test titletest title `, styles: [] }) export class AppComponent { madaoSrc = '../assets/images/madao.jpg'; customTitle = 'bbb'; }
Copy after login
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
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!
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