Angular Material Tutorial - 8 - Badges

Codevolution
28 Feb 201908:03

Summary

TLDR本视频介绍了如何在Angular Material中使用徽章(Badges)。徽章是一种用于UI元素上显示状态描述的小组件,例如显示未读消息数量。首先,需要导入Badge模块,并在组件中添加。然后,通过在元素上使用matBadge属性来创建徽章,并可自定义其位置、大小和颜色。此外,还可以控制徽章与文本的重叠,并使用属性绑定和条件渲染来动态显示徽章。视频最后提供了官方文档链接,供进一步学习。

Takeaways

  • 📌 徽章(Badges)是用于UI元素的小型状态描述符,常用于显示未读消息数量或通知。
  • 🔨 在Angular Material中使用徽章前,需要先导入Badge模块。
  • 📦 徽章模块通过`import MatBadgeModule from '@angular/material/badge'`的方式导入。
  • 🎯 徽章通过`matBadge`属性附加在HTML元素上,并通过属性值设置徽章显示的内容。
  • 🔄 徽章的位置可以通过`matBadgePosition`属性进行控制,如上、下、前、后等。
  • 📈 徽章的大小可以通过`matBadgeSize`属性调整,有小、中、大三种选项。
  • 🎨 徽章的背景颜色可以通过`matBadgeColor`属性设置,颜色值依据应用主题而定。
  • 🚫 徽章与文本的重叠可以通过`matBadgeOverlap`属性控制,建议图标重叠而文本避免重叠。
  • 🔗 徽章的值可以通过属性绑定与类属性关联,实现动态显示。
  • 🔄 根据条件渲染徽章,例如当通知数量为零时可以隐藏徽章,使用`matBadgeHidden`属性实现。
  • 📚 更多关于Angular Material徽章的信息和API可以在material.io网站上找到。

Q & A

  • Angular Material徽章是什么?

    -Angular Material徽章是一种用于用户界面元素的小状态描述符,例如显示用户的通知数量或收件箱中的未读消息数量。

  • 如何在Angular中使用Material徽章?

    -要使用Angular Material徽章,首先需要在material.module.ts文件中导入MatBadgeModule,并将其添加到材料组件数组中。

  • 如何在HTML中创建一个基本的徽章?

    -在AB组件的HTML中,可以通过在一个span标签中使用matBadge属性并赋予一个值来创建一个基本的徽章。例如,使用`<span matBadge="5">通知</span>`。

  • 如何改变Angular Material徽章的位置?

    -可以通过matBadgePosition属性来控制徽章的位置。例如,设置`matBadgePosition="below after"`将徽章放置在元素下方的右侧。

  • Angular Material徽章有哪些尺寸选项?

    -徽章有三种尺寸选项:small(小)、medium(中,默认值)和large(大)。可以通过matBadgeSize属性来指定尺寸。

  • 如何设置Angular Material徽章的背景颜色?

    -可以通过matBadgeColor属性来设置徽章的背景颜色。可能的值为primary(主色)、accent(强调色)和warn(警告色),具体颜色取决于应用使用的主题。

  • Angular Material徽章是否可以与文本重叠?

    -徽章可以与文本重叠,但这通常不建议用于文本,而更适合图标。可以通过matBadgeOverlap属性来控制是否重叠,设置为false可以使徽章不与文本重叠。

  • 如何在Angular中绑定徽章的值?

    -可以通过属性绑定来设置徽章的值。例如,在app.component.ts中定义一个notifications属性,并在HTML中使用`[matBadge] = "notifications"`来绑定该属性。

  • 如何在Angular中根据条件渲染徽章?

    -可以使用matBadgeHidden属性来根据条件渲染徽章。例如,当notifications的值为0时,设置`[matBadgeHidden] = "notifications === 0"`可以隐藏徽章。

  • Angular Material徽章的文档在哪里可以找到?

    -可以在material.io的组件部分下找到徽章的文档,具体在buttons and indicators部分的API标签页。

  • 在Angular Material徽章中,如何根据通知数量动态显示和隐藏徽章?

    -可以通过绑定一个变量(如notifications)到matBadge属性,并使用matBadgeHidden属性结合条件判断(如notifications的数量)来动态控制徽章的显示和隐藏。

  • Angular Material徽章的默认位置和尺寸是什么?

    -Angular Material徽章的默认位置是放置在关联元素的上方右侧,即top right。默认尺寸是medium。

Outlines

00:00

📌 介绍Angular Material徽章

本段落介绍了Angular Material徽章的基本概念和使用场景。徽章是一种小型的状态描述符,用于用户界面元素,如显示用户的通知数量或收件箱中的未读消息。首先,需要导入Angular Material的徽章模块,并将其添加到材料组件数组中。然后,通过在HTML中使用mat badge属性,可以轻松创建徽章。此外,还展示了如何通过mat badge position属性来控制徽章的位置,以及如何通过mat badge size属性来设置徽章的大小。

05:02

🎨 定制Angular Material徽章

这一部分详细讲解了如何定制Angular Material徽章的外观。首先,通过mat badge color属性可以设置徽章的背景颜色,颜色值可以是主题中定义的主要颜色、强调色或其他颜色。其次,讲解了如何通过mat badge overlap属性控制徽章是否与文本重叠,建议对于图标重叠徽章可能是一个好选择,但对于文本则应尽量避免重叠。最后,介绍了如何通过属性绑定和条件渲染来动态显示徽章的值和根据条件隐藏徽章。

Mindmap

Keywords

💡material badges

Material badges 是一种用户界面组件,用于在网页或应用程序中展示状态描述信息。在视频中,它们被用来展示未读消息的数量或用户的通知等。

💡Bootstrap

Bootstrap 是一个流行的前端开发框架,它提供了一套响应式设计的网页模板和组件。对于熟悉Bootstrap的开发者来说,Angular Material badges 的概念和用法会非常相似。

💡Angular Material

Angular Material 是基于Angular框架的一套实现Material Design规范的UI组件库。它提供了一系列丰富的组件,包括按钮、输入框、导航栏等,用于构建高质量的用户界面。

💡mat badge attribute

mat badge attribute 是Angular Material中用于添加badge的属性。通过将该属性添加到HTML元素上,可以创建一个badge,并设置其显示的值。

💡position

在Angular Material badges中,position 属性用于控制badge的位置。默认情况下,badge位于关联元素的右上角,但可以通过设置mat badge position属性来改变其位置,如'above after'、'below before'等。

💡sizing

Sizing 指的是在Angular Material badges中可以设置badge的大小。通过mat badge size属性,可以选择'small'、'medium'或'large'三种尺寸,其中'medium'是默认值。

💡coloring

Coloring 在Angular Material badges中指的是可以设置badge的背景颜色。通过mat badge color属性,可以选择'primary'、'accent'或'warn'等颜色,具体的颜色值取决于应用的主题。

💡overlap

Overlap 在Angular Material badges中表示badge是否可以与文本或其他内容重叠。通过mat badge overlap属性,可以控制badge是否覆盖在关联元素的文本上,默认情况下是重叠的。

💡binding values

Binding values 指的是将badge的值绑定到类属性上,从而动态地更新badge显示的内容。在Angular中,可以使用属性绑定来实现这一点。

💡conditional rendering

Conditional rendering 是指根据条件来决定是否渲染某个UI组件。在Angular Material badges中,可以使用mat badge hidden属性来根据条件隐藏或显示badge。

💡material documentation

Material documentation 是指官方提供的Angular Material组件库的文档,其中包含了组件的使用方法、API、示例等重要信息。

Highlights

介绍Angular Material徽章(Badges)的基本用途和功能

徽章是用于UI元素的小型状态描述符,例如显示用户的通知或收件箱中未读消息的数量

使用Angular Material徽章前需要先导入徽章模块

创建第一个徽章,使用`matBadge`属性并设置要显示的值

徽章默认显示在关联元素的右上角

通过`matBadgePosition`属性控制徽章的位置

使用`matBadgeSize`属性可以指定徽章的大小,可选值为small、medium和large

通过`matBadgeColor`属性设置徽章的背景颜色,可选值为primary、accent和warn

使用`matBadgeOverlap`属性控制徽章是否与文本重叠

建议对于图标重叠徽章,对于文本尽量避免重叠

通过属性绑定动态设置徽章的值

使用`matBadgeHidden`属性基于条件渲染徽章

当通知数量为零时,隐藏徽章

提供了官方文档链接,方便用户查阅更多关于Angular Material徽章的信息

视频结束时提醒观众订阅,以便不错过下一期内容

Transcripts

00:00

let's take a look at material badges in

00:03

this video if you work with bootstrap

00:06

badges should seem familiar to you

00:09

they are basically small status

00:11

descriptors for UI elements for example

00:16

if you have to display notifications for

00:18

a user or the number of unread messages

00:21

in your Inbox badges are the way to go

00:24

so let's see what is possible with

00:27

angular material badges in this video to

00:30

be able to use them we first need to

00:33

import the badge module in material dot

00:37

module dot es import mat badge module

00:42

from angular slash material slash badge

00:46

and then add it to the material

00:49

components array

00:51

now let's create our first badge in AB

00:55

component dot HTML I'm going to include

00:58

a span tag with the text notifications

01:03

to add a badge to the span tag we make

01:07

use of the mat badge attribute

01:12

so mad badge and this attribute is equal

01:16

to the value you want to indicate let's

01:19

go with five if we save this and take a

01:23

look at the browser you should see the

01:25

badge in action the small circular

01:28

element on the top right of the span

01:30

text now this is the most basic badge

01:34

you can have let's see how to customize

01:37

this and let's start with the position

01:40

by default a badge is placed above and

01:45

after the element it is associated with

01:48

that is to the top right we can control

01:52

the position using the mat badge

01:55

position attribute so back in the HTML

01:58

I'm going to make three more copies of

02:01

this div tag

02:06

and I'm going to specify the Matt badge

02:09

position attribute

02:17

below and before

02:21

below and after

02:26

above and before let me also add some

02:30

margin to the div tags to make sure that

02:32

there is enough spacing between the

02:34

elements so div margin three realms now

02:41

if you save the files and take a look at

02:43

the browser you can see the different

02:46

positions above and after below and

02:50

before below and after above and before

02:56

so Matt badge position attribute to

02:59

control the position of the badge next

03:03

let's talk about badge sizing we can

03:06

specify the size using the Matt badge

03:09

size attribute the possible values are

03:12

small medium and large with medium being

03:16

the default value I'm going to make

03:20

three more copies of our notification

03:22

badge and for the first one I'm going to

03:26

add Matt badge size is equal to small

03:31

the second one medium which is also the

03:34

default and for the third one large if

03:39

you now save the file and take a look at

03:41

the browser you can see the badges which

03:43

are small medium and large

03:47

alright next we have badge coloring the

03:51

badge background color can be set using

03:54

the mat badge color attribute possible

03:58

values our primary accent and born with

04:02

primary being the default value the

04:05

color of course is determined by the

04:07

theme your application is using so back

04:11

in vs code I'm going to make three more

04:13

copies of the original notification

04:16

badge

04:20

for the first one I'm going to add math

04:23

badge color is equal to primary which is

04:27

the default for the second one it is

04:30

going to be accent and for the third one

04:32

it's going to be warned if we save this

04:36

and take a look at the browser you can

04:38

see primary accent and worn colors based

04:42

on your theme

04:44

now you might have noticed in all the

04:46

notification badges the badge overlaps

04:49

with the text the overlap flag though

04:53

can be controlled as well we specify the

04:56

Matt badge overlap attribute and set it

04:58

defaults so back in vs code on the last

05:02

badge I'm going to add the attribute

05:06

Matt batch overlap is equal to false if

05:11

you go back to the browser you can see

05:14

that the batch doesn't overlap with the

05:16

text anymore

05:17

in fact material documentation advises

05:21

us to overlap badges for icons and not

05:25

for text for example if you have the

05:28

inbox icon then overlapping might be a

05:31

good choice for text however try

05:34

avoiding overlap as much as possible

05:38

finally let's talk about binding values

05:41

and also conditional rendering the value

05:44

for a batch is usually stored in a class

05:47

property to bind the value we simply use

05:50

property binding so I'm going to open

05:53

app component es and over here I'm going

05:58

to create a new property notifications

06:02

is equal to two and then in the HTML on

06:07

the very first badge I'm going to have

06:10

property binding on the mat batch

06:12

attribute so square brackets for

06:16

property binding and the property we

06:18

want to bind to is notifications

06:23

this property right here so now if we go

06:27

back to the browser

06:28

the very first badge now indicates two

06:32

now sometimes you might also want to

06:34

conditionally render based on the number

06:37

of notifications for example when

06:39

notifications is zero it doesn't really

06:42

make sense to indicate that we rather

06:45

hide the badge itself we can do that

06:49

using Mod Podge hidden attribute so back

06:52

an app component I'm going to add the

06:55

condition property binding with mat

06:58

badge hidden

07:02

and this is going to be equal to the

07:05

condition notifications is equal to zero

07:10

now if we take a look at the browser you

07:15

can still see the badge to go back to vs

07:19

code change the value to zero

07:23

and you can see that the batch is now

07:26

hidden that is how you conditionally

07:29

render a batch in angular material using

07:33

the matte batch hidden attribute alright

07:37

that's pretty much what I have about

07:39

badges

07:40

let me quickly point you to the

07:42

documentation so go to material dot io

07:45

components under buttons and indicators

07:49

there is badge and if you go to the API

07:52

tab you will know what has to be

07:55

imported alright then thank you guys for

07:58

watching don't forget to subscribe I'll

08:00

see you guys in the next video

Rate This

5.0 / 5 (0 votes)

相关标签
Angular教程Material设计徽章应用UI元素前端开发徽章定制响应式设计状态指示通知显示编程教学