#09 Styling View Template | Angular Components & Directives| A Complete Angular Course

procademy
30 May 202313:06

Summary

TLDRThis lecture explains how to style the HTML of a view template in Angular. The speaker demonstrates styling elements using the 'Styles' property by applying CSS directly in the component's metadata. Additionally, they cover styling through external CSS files using the 'styleUrls' property for better maintainability. The differences between these methods are discussed, highlighting the drawbacks of inline styles and emphasizing the importance of component-specific CSS. Finally, the lecture clarifies that styles applied to a parent component do not affect child components, unlike in React.

Takeaways

  • 💻 The view template of a component is the HTML structure associated with it, and it can be styled in multiple ways.
  • 🔗 The 'Styles' property in the metadata object allows for direct styling of the HTML elements within the view template.
  • 🚫 CSS styles like `text-decoration: none` can be applied to remove the underline from anchor elements and add margin between them.
  • 🖱️ Button elements can also be styled with CSS directly within the Styles array, like adding padding to the button.
  • 📏 Classes and IDs can be used to style specific elements within the component, such as a `div` element using its class name.
  • ⚠️ A major downside of using the 'Styles' array is the mixing of TypeScript and CSS, making the code harder to maintain.
  • 🔍 Errors in the 'Styles' array won’t be caught during compile time, but only during runtime, posing debugging challenges.
  • 📝 The 'styleUrls' property is a better approach for managing styles, as it allows linking to external CSS files, improving maintainability.
  • 🌐 Styles defined in the 'styleUrls' will only apply to the specific component they are associated with and not affect child components.
  • 💡 In Angular, unlike React, styles applied to a parent component do not automatically propagate to child components, ensuring scoped styling.

Q & A

  • What is a view template in Angular?

    -A view template in Angular refers to the HTML file that defines the structure and layout of a component's user interface. For example, the 'header.component.html' file is the view template for the 'header' component.

  • How can you style HTML elements in a view template using the 'Styles' property?

    -You can use the 'Styles' property within the component's metadata object to directly style HTML elements. The 'Styles' property accepts an array of strings, where each string contains the CSS styles for a specific HTML element. For example, to remove the underline from anchor tags, you can define CSS like 'text-decoration: none;' within the array.

  • What are the limitations of using the 'Styles' property in Angular?

    -The main limitations are: 1) Mixing CSS with TypeScript code, which is not a good practice and makes code less maintainable. 2) CSS is treated as a string, so syntax errors are only detected at runtime, not compile time. 3) If the CSS grows, the array becomes large and unmanageable.

  • How can you style elements using the 'styleUrls' property in Angular?

    -Instead of using the 'Styles' property, you can use the 'styleUrls' property to reference external CSS files. This is done by specifying the path to a CSS file within an array. The styles defined in this external file will be applied to the component's view template.

  • What are the benefits of using 'styleUrls' over 'Styles'?

    -The main benefits are: 1) Keeping the CSS separate from TypeScript, which improves maintainability. 2) Syntax errors in CSS can be caught during compile time. 3) The CSS can be written in external files, which makes it easier to manage larger amounts of styling code.

  • How does Angular apply styles using 'styleUrls'?

    -When using the 'styleUrls' property, the styles in the referenced CSS file are scoped to the component's view template. This means the styles will only affect the HTML within the component and will not affect other components.

  • Can styles from a parent component affect a child component in Angular?

    -No, in Angular, styles applied in a parent component (such as in its CSS file) do not automatically propagate to child components. Each component's styles are scoped to that component unless explicitly shared.

  • How would you style an HTML element by its class name in Angular?

    -You can style an HTML element by its class name in the 'Styles' array by using a dot (.) followed by the class name, and then defining the styles inside curly braces. For example, '.ecat-header { width: 100%; height: 70px; }' styles the div with the 'ecat-header' class.

  • What happens if you miss a colon or semicolon while using the 'Styles' property?

    -If you miss a colon or semicolon in the 'Styles' property, no error will be shown at compile time because the CSS is treated as a string. The error will only surface at runtime when the styles fail to apply correctly.

  • Can multiple CSS files be used in the 'styleUrls' property?

    -Yes, multiple CSS files can be used in the 'styleUrls' property by adding their file paths as strings in the array, separated by commas. Each specified file's styles will be applied to the component's view template.

Outlines

00:00

📚 Introduction to Styling View Templates

In this paragraph, the speaker introduces the concept of styling view templates in Angular. They explain that in the previous lesson, the focus was on creating view templates, and in this lesson, the focus shifts to how to style the HTML within those templates. The example used is a header component, where the content of the `header.component.html` is rendered when the `app-header` selector is used.

05:03

🎨 Using the Styles Property

The speaker demonstrates how to style HTML elements using the `Styles` property in Angular. By defining styles directly in the metadata object, they show how to remove underlines from anchor elements and add margins between them. Similarly, padding is added to a button element. The explanation includes step-by-step instructions on defining styles using CSS within the `Styles` array, making the HTML elements appear more visually appealing.

10:05

📏 Styling Elements by Class and ID

This section shows how to style HTML elements by targeting class and ID selectors. The speaker explains how to style a `div` element with a class of `ecat-header` and modify its width and height. Additionally, they mention how to style elements by their IDs, providing a clear understanding of how CSS can be applied to elements in the view template using the `Styles` array.

⚠️ Disadvantages of the Styles Property

The speaker discusses the downsides of using the `Styles` property in Angular. The first drawback is mixing CSS with TypeScript, making the code less maintainable. Secondly, there’s no compile-time error detection for incorrect CSS, so errors are only caught at runtime. Finally, if the styles grow, the array can become large and messy. These issues make maintaining large codebases difficult.

🗂️ Transition to Style URLs Property

In this part, the speaker introduces the `styleUrls` property as an alternative to the `Styles` array. Instead of writing CSS directly in the metadata, external CSS files are used, leading to cleaner and more maintainable code. The speaker walks through the process of creating a `header.component.css` file, writing styles within it, and linking it to the component via the `styleUrls` array. This method helps organize CSS for large projects.

🎯 Scope of Styles in Angular Components

Here, the speaker highlights how CSS styles in Angular are scoped to individual components. They explain that styles defined in a component's CSS file apply only to that component, and not to other components or their child components. This isolation is demonstrated by comparing a button in both the `app` and `header` components, showing how styles from one component don’t affect elements in another.

💡 Conclusion on Styling View Templates

The final paragraph summarizes the lesson on styling view templates using both the `Styles` property and `styleUrls`. The speaker reiterates the benefits of using `styleUrls` for better code maintainability and advises avoiding the `Styles` array for large or complex stylesheets. They encourage the viewer to ask questions if needed and conclude the lecture.

Mindmap

Keywords

💡View Template

A View Template in Angular refers to the HTML code associated with a component. It defines how the component's layout and content are structured. In the video, the header component's view template is described as the `header.component.html` file, and it's rendered whenever the `app-header` selector is used.

💡Styles Array

The Styles Array is a property in Angular's component metadata used to directly define CSS styles within the component's TypeScript file. It allows developers to add CSS rules inline for specific elements. The video explains that while this method allows for immediate styling, it has several disadvantages such as mixing CSS with TypeScript, making the code less maintainable.

💡styleUrls

The `styleUrls` property allows Angular developers to link external CSS files to a component. This method enhances maintainability by keeping the CSS code separate from TypeScript. The video highlights that instead of embedding styles directly using the Styles array, it’s better to reference external files using `styleUrls`, improving code organization.

💡Component

In Angular, a Component is a building block that contains a template (HTML), logic (TypeScript), and styles (CSS). The header component is the primary example in the video, and its view template, style, and behavior are encapsulated in files like `header.component.html`, `header.component.css`, and the component’s TypeScript file.

💡CSS

Cascading Style Sheets (CSS) is a language used to style HTML elements. In the video, CSS is applied to the view template either through the Styles array or by linking external files via `styleUrls`. For example, CSS rules are created to remove the underline from anchor links and to set margins between them.

💡Curly Braces

Curly Braces `{}` are used in CSS to encapsulate style declarations. In the video, these braces are mentioned when the speaker shows how to apply CSS styles inside the Styles array for specific elements such as anchor tags and buttons.

💡Anchor Element

An anchor element (`<a>` tag) is used to create hyperlinks in HTML. The video shows how to style anchor elements within the header component’s view template, removing the default underline and adjusting margins through CSS.

💡Class Selector

A Class Selector in CSS is used to apply styles to elements with a specific class. In the video, the speaker explains how to style an element by targeting its class, such as styling the div element with the class `ecat-header` using the format `.ecat-header {}`.

💡Disadvantages of Styles Array

The Styles array, while useful, comes with drawbacks. The video outlines three main disadvantages: 1) It mixes CSS with TypeScript, 2) Errors are not caught until runtime, and 3) It becomes unmanageable when there are many styles. These factors make it less preferable compared to `styleUrls`.

💡Component-specific Styles

Component-specific Styles in Angular mean that styles defined within a component only affect that component's template. The video emphasizes this point by explaining how styles in the `header.component.css` apply only to the header component and do not affect other components or child elements.

Highlights

Introduction to styling a view template in Angular using the 'Styles' and 'styleUrls' properties.

The 'Styles' property allows you to define inline CSS for elements within the view template using an array.

Example of removing underlines from anchor elements and adding margin between them using inline CSS in the 'Styles' property.

Demonstration of adding padding to a button element by defining styles for the button tag within the 'Styles' array.

Usage of class selectors (e.g., '.ecat-header') and ID selectors for styling elements in the view template.

Disadvantages of the 'Styles' array include mixing CSS with TypeScript code, lack of compile-time error checking, and poor maintainability for large CSS code.

Introduction to the 'styleUrls' property as an alternative to 'Styles' to maintain CSS in a separate file, improving maintainability.

Creation of a separate CSS file (header.component.css) and linking it to the header component using 'styleUrls'.

Demonstration of how CSS in the linked file applies only to the view template of the associated component (header component).

Illustration of the scope of styles in Angular, showing that styles applied in one component do not affect child components or other components.

Comparison of Angular's component-scoped styling with React, where parent component styles can affect child components.

Example of adding a button in the parent 'App' component and styling it independently from the button in the 'Header' component.

Explanation that styles defined in one component (e.g., app.component.css) do not apply to child components like the header.

Removal of the test CSS and button element used in the demonstration, returning the code to its original state.

Conclusion and summary of the key differences between 'Styles' and 'styleUrls' for managing CSS in Angular.

Transcripts

play00:00

in the last lecture we learned what is a

play00:02

view template and what are the different

play00:04

ways in which we can create a view

play00:05

template for a component now in this

play00:07

lecture we are going to learn how we can

play00:09

style the HTML of a view template

play00:12

so here we have our header component and

play00:15

for this header component this header

play00:17

component.html file is its view template

play00:20

so wherever we will use this app header

play00:22

selector there the content of this

play00:24

header component.html file will be

play00:26

rendered so if I go to the web page

play00:29

you can see the HTML of that file has

play00:31

been rendered and it looks something

play00:32

like this now currently this HTML is not

play00:36

styled so in this lecture we are going

play00:38

to learn how we can style this HTML

play00:40

so let's go back to vs code

play00:43

and in order to style a view template

play00:46

to this metadata object we can specify

play00:49

another property called Styles or style

play00:52

urls

play00:54

so let's first talk about Styles

play00:55

property

play00:57

so using this property we can style our

play01:00

view template now to this Styles

play01:02

property we need to assign an array and

play01:04

inside this array we can Define the

play01:06

style for different HTML elements for

play01:08

example if I go to this header

play01:09

component.html

play01:11

there we have this anchor element So

play01:14

currently if I go to the web page you

play01:16

see these anchor elements these links

play01:18

are underlined but I don't want this

play01:20

underline for each of these links and I

play01:22

also want to add some margin between

play01:24

these links

play01:26

so let's go ahead and let's do that for

play01:28

that what we will do is

play01:30

inside this array we will specify a

play01:32

string using single quotes like this and

play01:35

in there we will specify the HTML

play01:37

element which we want to style here we

play01:39

want to style the anchor element

play01:41

and after that we'll specify a set of

play01:44

curly braces like this and inside this

play01:46

we can specify the CSS Styles so the

play01:48

first thing which I want to do is I want

play01:50

to set the text decoration To None I

play01:53

don't want the underline for these links

play01:56

okay then I can use semicolon and then I

play01:58

can specify another style so I will set

play02:00

margin top bottom I will set 0 pixel and

play02:03

left right 10 pixel

play02:06

let's see if the changes let's go back

play02:08

to the browser

play02:10

and now you will see those links are not

play02:12

underlined anymore and there is a margin

play02:14

between these links

play02:16

in the same way here we also have a

play02:18

button element and for this button

play02:19

element we don't have any value right

play02:21

now but what I want is for this button

play02:23

element I want to add some padding so I

play02:25

can go back

play02:27

and

play02:28

this style is for anchor element after

play02:31

this style we can add a comma

play02:34

and we can again use a set of single

play02:36

quotes like this and in there we can

play02:38

specify the style for some other element

play02:40

in this case now we want to specify the

play02:43

style for button element so after the

play02:46

element name we use a set of curly

play02:47

braces and in there we can specify the

play02:49

style so here I will simply add some

play02:51

padding

play02:52

maybe top bottom 10 pixel and left right

play02:55

20 pixel

play02:59

okay

play03:01

save the changes let's go back to the

play03:03

web page

play03:05

and you can see that styling has been

play03:06

added on this button element

play03:08

so this is how you can style your HTML

play03:11

elements using Styles array in the

play03:14

Styles array you specify the styles for

play03:16

HTML element like this so here first we

play03:19

are specifying the style for anchor

play03:21

element then when we wanted to style

play03:23

some other element we used a comma again

play03:25

we specified single quotes and inside

play03:28

that we are specifying the style for

play03:30

button element

play03:31

now let's say I also want to style this

play03:34

div

play03:35

and for this div the class is ecat

play03:38

header so using this class I want to

play03:39

style this div let's see how we can do

play03:42

that I'll copy this class name let's go

play03:44

back

play03:45

and again after this style after we have

play03:47

styled this button element let's use

play03:49

comma let's use another set of single

play03:51

quotes and now I want to style an

play03:54

element whose class is ecat header so

play03:57

when we want to style an element using

play03:59

its class name first we need to use a

play04:01

DOT and then we specify the class name

play04:04

and then a set of curly braces like this

play04:06

and in there we specify the CSS Styles

play04:09

so here let's say I want to set the

play04:11

width maybe to 100 percent and I want to

play04:15

set the height let's say to

play04:18

70 pixel okay so this is how we can

play04:22

style an element using its class name

play04:25

then if you want to style an element

play04:27

using its ID

play04:29

then what you need to do is you need to

play04:32

use a pound sign you need to specify the

play04:34

ID of the element and then you can

play04:36

specify the style for that

play04:39

so I hope you got the point so this is

play04:42

how we can style our view template using

play04:44

this Styles array

play04:46

now here using this Styles array we have

play04:49

a lot of disadvantage the first

play04:50

disadvantage is that we are mixing CSS

play04:53

with typescript code and that is not a

play04:55

good practice

play04:57

second disadvantage is that since these

play05:00

values are string values here if we miss

play05:03

something for example here if I miss

play05:04

this colon it is not going to show any

play05:07

error so in this case we are not going

play05:09

to have any compile time error we will

play05:12

come to know about these errors these

play05:14

issues only during runtime so this is

play05:16

another problem

play05:18

and third currently we have written CSS

play05:21

Styles only for three elements for this

play05:23

anchor element for this button element

play05:25

and for the div element whose class is

play05:28

ecat header

play05:29

but if we have to write a lot of CSS in

play05:33

that case this array will become huge

play05:35

and it will be less maintainable and it

play05:38

will just make our code a lot Messier

play05:40

and we don't want that right so what are

play05:43

the disadvantages we have with this

play05:44

tiles array

play05:46

first of all it mixes in CSS and

play05:48

typescript code which makes the code

play05:50

less maintainable

play05:52

then since CSS is written as a string

play05:54

using the Styles property if there is

play05:57

some error we will not know about it

play05:58

during compile time we will only come to

play06:00

know about it during runtime and then if

play06:03

the number of lines of CSS code is huge

play06:05

in that case it will become Messier and

play06:07

less maintainable

play06:09

and that's why instead of using Styles

play06:12

array what we can do is we can use style

play06:14

urls

play06:16

so here apart from this Styles property

play06:19

we also have another property called

play06:20

style URLs so let me remove this style

play06:23

from here

play06:25

and instead of style we can use

play06:27

style urls

play06:29

and the style URLs it is also an array

play06:32

but inside this array instead of writing

play06:34

CSS code what we do is we specify the

play06:37

path of a CSS file which can be used for

play06:40

the view template

play06:42

so what I will do here is inside this

play06:44

header folder I'll create a new file

play06:47

and again the name of the file should be

play06:49

the component name in this case header

play06:50

Dot component dot extension of the CSS

play06:55

file is dot CSS

play06:58

okay so here we have created header

play07:00

component.css file and inside this file

play07:02

we will write all the CSS which we want

play07:05

to apply on our view template

play07:07

and we will reference this file using

play07:10

this style URLs property so in this

play07:13

array we can specify a string value like

play07:15

this and in there we specify the path of

play07:17

the CSS file which we want to use as a

play07:19

style sheet so in the current directory

play07:21

we have this header dot component dot

play07:25

CSS file

play07:27

okay

play07:28

now here we are only using one CSS file

play07:31

for the style sheet but if you want you

play07:33

can use multiple CSS files all you have

play07:35

to do is you have to use comma and then

play07:36

you have to specify the path of another

play07:38

CSS file which you want to use here

play07:40

but here I only want to use one CSS file

play07:43

so I will specify that

play07:45

and now whatever CSS we write inside

play07:47

this file that will be applied to this

play07:50

HTML file okay so for this component

play07:54

this header component this file is its

play07:56

view template and this file is its style

play08:00

sheet okay so whatever CSS Styles we

play08:03

write here inside this file that will be

play08:05

applied to the HTML of this file

play08:08

now in order to save some time

play08:11

I have already written some CSS so I'll

play08:13

grab it from here

play08:16

okay let's go back to vs code and let's

play08:19

paste it inside this header

play08:21

component.css file

play08:23

so you see this much of CSS I have

play08:26

written just for header so if we would

play08:29

have been using Styles array instead of

play08:32

style URLs that array would have become

play08:34

quite lengthy and less maintainable

play08:36

that's why instead of using Styles

play08:38

property we are using style URLs and

play08:40

there we are simply specifying the path

play08:42

of the file and we are writing all our

play08:44

CSS in a separate file

play08:47

let's save this file

play08:49

and now what will happen is these CSS

play08:51

styles that will be applied to the HTML

play08:53

of header component.html file so now if

play08:56

we go to the browser

play08:59

this is how our application now looks

play09:01

like here we have created a header in

play09:04

that header we have some links we have a

play09:06

search bar and we have a button now this

play09:09

header is not yet styled properly that's

play09:12

because in the next lecture we are also

play09:14

going to apply some Global styles to our

play09:16

component currently what is happening is

play09:18

whatever style we have specified inside

play09:20

this header component.css that style

play09:24

will be only applied for header

play09:26

component.html

play09:27

this style will not be applied on any

play09:30

other HTML file

play09:32

and this is very important to understand

play09:34

in angular when you use style URLs

play09:37

property or Styles property and using

play09:40

that whatever Styles you apply that only

play09:43

gets applied to that particular view

play09:44

template

play09:45

in this case whatever style sheet you

play09:48

use here those Styles will only get

play09:51

applied to the view template of this

play09:52

header component these Styles will not

play09:55

get applied to any other component

play09:57

if any other component is using the same

play10:00

CSS file then only those Styles will be

play10:03

applied to that component

play10:04

but in this case since we are using this

play10:06

CSS file only for this header component

play10:09

this style will only be applied on this

play10:11

header component it will not get applied

play10:12

to any other component not even the

play10:15

child component of this header component

play10:17

okay so if you have worked with react

play10:20

then you know that in react when you

play10:22

apply some styles on the parent

play10:24

component those Styles will also get

play10:26

applied to its child components but

play10:28

that's not the case here let me actually

play10:30

show you that so what I will do is I

play10:32

will go to app component.html

play10:35

and in there I will add a button element

play10:40

okay here let me simply say click me

play10:45

so here I have this button element and

play10:47

this header component it is the child

play10:49

component of app component

play10:51

right now if I go to this app

play10:53

component.css and if I add some CSS

play10:56

style for the button

play10:58

maybe I will simply say

play11:00

background color should be red for all

play11:03

the buttons inside this app component

play11:06

then we also have the button element in

play11:09

our header component so if I go to

play11:11

header component.html here also you have

play11:13

the button element and this header

play11:16

component it is basically a child

play11:18

component of this app component

play11:19

so when I'm specifying some styles for

play11:23

the button element in the app component

play11:25

what do you think will it get applied to

play11:27

the child components button element also

play11:30

let's see that let's save the changes

play11:33

let's go back to the web page

play11:35

and there you will see we have this

play11:37

button element and this button element

play11:38

is coming from App component and there

play11:41

the background color of this button

play11:42

element is red

play11:44

but here also we have a button element

play11:46

but this button elements background

play11:48

color has not changed

play11:50

and that's what I meant when I said that

play11:52

whatever style we specify for a

play11:55

component using Styles property or style

play11:57

URL property

play11:59

in this case for this app component we

play12:02

are specifying the style using app

play12:04

component.css file so whatever style we

play12:06

will specify inside this app

play12:07

component.css file that style will only

play12:10

get applied to the HTML file of that

play12:12

component

play12:14

that style will not get applied to any

play12:16

other HTML in this case the style which

play12:19

we have specified here inside this app

play12:21

component.css it is only applicable on

play12:23

app component.html this style for this

play12:26

button element is not applicable on

play12:29

header component.htmls button

play12:31

okay

play12:33

all right so let me go ahead and let me

play12:35

remove this CSS style from here let's

play12:37

close this file

play12:38

and let's also remove this button

play12:40

element from here

play12:44

all right

play12:46

so in this lecture we learned how we can

play12:48

style our view template using Styles

play12:51

property or style URLs property

play12:55

dissolve from this lecture if you have

play12:57

any questions then feel free to ask it

play12:59

thank you for listening and have a great

play13:01

day

Rate This

5.0 / 5 (0 votes)

相关标签
Angular StylingView TemplatesComponent DesignCSS in AngularWeb DevelopmentFrontend TipsCode Best PracticesTypescript CSSUI StylingResponsive Design
您是否需要英文摘要?