Home > Web Front-end > JS Tutorial > A brief discussion on class and style binding in Angular10

A brief discussion on class and style binding in Angular10

青灯夜游
Release: 2021-03-23 10:44:51
forward
2280 people have browsed it

This article will introduce to you the class and style binding in Angular. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

A brief discussion on class and style binding in Angular10

Binding of class and style in Angular10

1.classBinding

Binding type Syntax Input type Input value example
Single class binding [class.foo]='flag' boolean|undefined|null true,false
Multiple class binding [class]='classExpr' string
{[key :string]:boolean | undefined | null}
Array
'my-class1 my-class2'
{foo: true, bar: false}
['foo ','bar']

Related recommendations: "angular tutorial"

1.1 Single attribute binding

1. Basic syntax

<p>
    <button>修改flag的值</button></p>
Copy after login

2. When the value of the expression is true, Angular will add this class, and when it is false, it will remove

flag = truechangeFlag():void {
    this.flag = !this.flag}
Copy after login

3. When flag is true
A brief discussion on class and style binding in Angular10

4. When flag is false
A brief discussion on class and style binding in Angular10

1.2 Multiple attribute binding - string form

1, string form

<p>绑定字符串的class</p>
Copy after login
classExpr:string = 'class-expr1 class-expr2 class-expr3'
Copy after login

2, binding result

A brief discussion on class and style binding in Angular10

1.3 Multiple attribute binding - object form

1, object form

<p>绑定对象形式的class</p>
Copy after login
classExpr:object = {
    'foo': true,
    'bar': false}
Copy after login

2, binding result

A brief discussion on class and style binding in Angular10

1.4 Multiple attribute binding-array form

1, array form

<p>绑定数组形式的class</p>
Copy after login
classExpr:Array<string> = ['foo','bar']</string>
Copy after login

2, binding result

A brief discussion on class and style binding in Angular10

2. styleBinding

Binding type Syntax Input type Input value example
Single style binding [style.width]=“width” string undefined null "100px"
Single style binding with units [style.width.px]="width" number undefined null 100
Multiple style bindings [style]="styleExpr" string
{[key: string]: string undefined null}
"width: 100px; height: 100px"
{width: '100px', height: '100px'}

2.1 Single attribute

1. The form of a single attribute

<p>绑定单个形式的style</p>
Copy after login
Copy after login
styleExpr:string = '100px'
Copy after login

2. Binding result

A brief discussion on class and style binding in Angular10

2.2 Single attribute with unit

1, with unit

<p>绑定单个形式的style</p>
Copy after login
Copy after login

2, binding result

A brief discussion on class and style binding in Angular10

2.3 Multiple attributes Binding

<p>绑定多个形式的style</p>
Copy after login

1, string

styleExpr:string = 'width: 100px;height: 200px'
Copy after login

2, object

styleExpr:object = {
    width: '100px',
    height: '200px'}
Copy after login

3, result graph

A brief discussion on class and style binding in Angular10

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of A brief discussion on class and style binding in Angular10. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template