#18 Two way Data Binding | Angular Components & Directives | A Complete Angular Course

procademy
6 Jun 202315:47

Summary

TLDRThis lecture explains the concept of two-way data binding in Angular. It builds on previous discussions of one-way data binding (property and event binding) by demonstrating how to bind data in both directions—between the component class and the view template. The example provided includes creating a search box component, showcasing how input values update both the view and component class in real time. The video also highlights using Angular's built-in 'ngModel' directive to simplify two-way data binding. This foundational knowledge is essential for implementing dynamic and interactive forms in Angular applications.

Takeaways

  • 🔗 One-way data binding involves binding data either from the component class to the view template or vice versa, but not both.
  • 🔧 String interpolation and property binding are used to bind data from the component class to the view template.
  • 🎯 Event binding is used to bind data from the view template back to the component class.
  • 🛠 Two-way data binding allows for data to flow both ways between the component class and the view template, reflecting changes in both directions.
  • 📝 An example of two-way data binding is implemented with a search text box in an e-commerce application, where user input updates a property in the component class.
  • 📑 The 'search' component is created to handle the search functionality, including a text box and a button.
  • 🖌 CSS is used to style the search component for better user interface.
  • 🔑 The 'search-text' property in the component class is used to prepopulate the input element in the view template.
  • 🔄 Two-way data binding is achieved by combining property binding for setting the initial value and event binding for updating the property when the user types in the input box.
  • 📚 The 'FormsModule' from 'angular/forms' must be imported and added to the 'imports' array in the app module to use 'ngModel' for two-way data binding.
  • 🔄 Using 'ngModel' simplifies the syntax for two-way data binding, eliminating the need to explicitly combine property and event bindings.

Q & A

  • What is one-way data binding?

    -One-way data binding refers to binding data in a single direction, either from the component class to the view template or from the view template to the component class. It is achieved through string interpolation and property binding for component-to-view data flow and event binding for view-to-component data flow.

  • What are the two main types of one-way data binding?

    -The two main types of one-way data binding are property binding, where data is passed from the component class to the view template, and event binding, where data is passed from the view template to the component class.

  • What is two-way data binding?

    -Two-way data binding allows data to flow in both directions: from the component class to the view template and from the view template back to the component class. It combines both property binding and event binding.

  • How can two-way data binding be implemented?

    -Two-way data binding can be implemented by combining property binding and event binding. It can also be achieved using the built-in Angular directive 'ngModel', which simplifies the process.

  • What is the role of the 'ngModel' directive in two-way data binding?

    -The 'ngModel' directive is used to achieve two-way data binding in a simplified manner. It binds an input element to a component property, allowing changes in the view to update the component property and vice versa.

  • What error might you encounter when using 'ngModel' and how can it be fixed?

    -When using 'ngModel', an error stating 'Can't bind to ngModel since it isn't a known property of input' may occur. This can be fixed by importing the 'FormsModule' from '@angular/forms' and registering it in the 'Imports' array of the module.

  • What is the initial step when starting to implement the search functionality in the application?

    -The initial step is to create a new search component inside the 'product-list' component using Angular's 'ng generate' command.

  • How can you pre-populate an input element with a property value in Angular?

    -To pre-populate an input element with a property value, you can use one-way property binding by binding the input element's 'value' property to the desired component property using square brackets, e.g., '[value] = searchText'.

  • What does event binding achieve in this context?

    -Event binding allows the input event in the text box to update the corresponding component property (e.g., 'searchText') when the user types something in the text box. It ensures that changes in the view are reflected in the component class.

  • What is the purpose of combining property and event binding?

    -Combining property and event binding allows two-way data binding, enabling the data to be synchronized between the component class and the view template in both directions. This ensures that any change made in the view is reflected in the component and vice versa.

Outlines

00:00

📘 Understanding One-Way and Two-Way Data Binding

This paragraph introduces the concept of one-way data binding, where data flows in one direction: from the component class to the view template or vice versa. It explains that one-way data binding is achieved through string interpolation and property binding (component to view) or event binding (view to component). It then transitions to two-way data binding, which allows data flow in both directions. Two-way binding ensures changes in the component class reflect in the view and vice versa.

05:00

🔍 Implementing a Search Box in an E-Commerce App

The focus here is on implementing a search functionality in an e-commerce application. The steps include generating a new component for the search box, moving to the product list component, and setting up the search component. Basic HTML elements (input field, button, and paragraph) are added to the search component. To style the HTML, CSS is applied, and the selector for the search component is used in the product list component to integrate the search box functionality.

10:03

💻 Property Binding for Prepopulating Search Text

This paragraph covers how to use property binding to prepopulate the input field with default text ('Men's VI'). The search text property is defined in the component class, and its value is bound to the input element using property binding. The same search text is displayed in a paragraph element to reflect the search result dynamically. The example demonstrates how changing the value in the component class updates the view template automatically.

15:05

⌨️ Event Binding to Update the Component Class

Here, event binding is introduced to update the search text in the component class when a user types in the input field. An `input` event is bound to a method (`updateSearchText`) that captures the user's input and assigns it to the search text property. The target property of the event object is used to access the value from the input element. This dynamic binding allows the view template to update the component class in real-time as the user interacts with the search box.

🔄 Simplifying Two-Way Data Binding with NG Model

The paragraph explains how to simplify two-way data binding using Angular's built-in `ngModel` directive, which combines property binding and event binding. Instead of manually implementing both types of binding, `ngModel` can be used to achieve the same result with less code. The `FormsModule` must be imported and registered in the app module to enable `ngModel`. With this, the input field is bound to the search text property, allowing seamless data updates between the view and component.

✅ Finalizing Two-Way Data Binding

This concluding section reinforces the concept of two-way data binding. The search text is dynamically updated in both the input field and the component class. As the user types, changes in the input field reflect in the search text property and vice versa. This two-way data binding ensures synchronization between the component and view, completing the explanation of how two-way binding works in Angular.

Mindmap

Keywords

💡Data Binding

Data binding refers to the process of connecting the component class's properties with the view template in an Angular application. In the video, the instructor explains different types of data binding, including one-way and two-way, and how they allow for interaction between the UI and the underlying logic.

💡One-Way Data Binding

One-way data binding allows data to flow in a single direction, either from the component class to the view template (using property binding and string interpolation) or from the view template to the component class (using event binding). The video explains that this technique is useful when you want data to either be displayed or updated without needing immediate feedback between the UI and the logic.

💡Two-Way Data Binding

Two-way data binding is a method in Angular that allows data to be synchronized between the component class and the view template. In the video, the instructor discusses how two-way data binding combines property and event binding, enabling changes in the UI to reflect in the component class and vice versa, providing a consistent and interactive user experience.

💡Property Binding

Property binding is used to pass data from the component class to the view template by binding properties in the HTML to the component’s values. This concept is discussed in the video to prepopulate an input element with a value from the component, showing how to dynamically control UI elements based on data.

💡Event Binding

Event binding allows the view template to communicate user actions (such as clicks or keystrokes) to the component class. The instructor uses an example of capturing user input in a text box and updating a property in the component class, showcasing how event binding helps create interactive elements.

💡ngModel

ngModel is an Angular directive used to achieve two-way data binding in a more efficient way. In the video, ngModel is introduced as a built-in directive that allows combining property binding and event binding in a simple syntax, helping developers keep form inputs synchronized with their component properties.

💡Component Class

A component class in Angular is responsible for defining the data and methods that interact with the view template. In the video, the instructor explains how component classes store properties like 'searchText' and define methods that manipulate the UI, which illustrates how they are essential for managing application state.

💡View Template

The view template in Angular is the HTML code that defines how the UI should look. It interacts with the component class to display data and listen to user actions. The video discusses how data from the component class is rendered on the view template using binding techniques, thus emphasizing the role of view templates in visual presentation.

💡FormsModule

FormsModule is an Angular module that provides directives like ngModel, which facilitate the creation of forms and two-way data binding. In the video, the instructor imports FormsModule to use ngModel for managing the input form's data binding, demonstrating how the module simplifies form-related development in Angular applications.

💡Search Functionality

Search functionality refers to the capability of filtering or finding products based on user input in an e-commerce application. The instructor in the video demonstrates how to create a search box using two-way data binding and ngModel, making it possible to instantly reflect user input within the component and show related products in the UI.

Highlights

Introduction to one-way data binding, where data flows in one direction: either from the component class to the view template or vice versa.

Explanation of string interpolation and property binding for binding data from the component class to the view template.

Discussion on event binding, which allows data to be passed from the view template to the component class, demonstrated through a search text box.

Introduction to two-way data binding, enabling data to flow from the component class to the view template and back, keeping both in sync.

Illustration of how property binding and event binding can be combined to achieve two-way data binding.

Practical example of implementing a search functionality for an e-commerce application, including creating a search component.

Explanation of how to bind a text input field in a search box using property binding to pre-populate the input with data from the component class.

Demonstration of binding user input events to the component class using event binding to capture and update search text dynamically.

Addressing how to resolve errors when binding event objects and dealing with potential null values during event handling.

Introduction of Angular's NG model directive, which simplifies two-way data binding without needing explicit property and event bindings.

Explanation of importing and registering Angular FormsModule to use NG model in a component.

Detailed step-by-step guide to incorporating NG model for the search input to allow seamless two-way data binding between the input element and component property.

Highlighting the significance of the two-way data binding feature in keeping the view and component synchronized in real-time.

Explanation of how two-way data binding enhances user experience by allowing dynamic interaction between UI elements and underlying data.

Final summary emphasizing that two-way data binding involves continuous synchronization between the view and the component class, making it easier to manage dynamic data in Angular applications.

Transcripts

play00:00

so in the last few lectures we learned

play00:02

about data binding from component class

play00:05

to view template and from view template

play00:07

to component class and this is called as

play00:10

oneway data binding because there we are

play00:12

only binding data in One Direction

play00:14

either from component class to view

play00:16

template or from view template to

play00:18

component class so in one way data

play00:21

binding either we can bind data from

play00:23

component class to view template we can

play00:25

achieve it using string interpolation

play00:26

and property binding and we have already

play00:28

talked about that then we can also bind

play00:30

data from view template to component

play00:33

class using event binding and we talked

play00:35

about event binding in our last lecture

play00:38

now we are going to talk about two-way

play00:39

data binding in two-way data binding we

play00:42

can bind data from component class to

play00:44

view template and at the same time the

play00:46

data will be binded from view template

play00:48

to component class that means whenever

play00:51

we change a property in the component

play00:53

class the changed value will reflect in

play00:55

the view and in the view whenever we

play00:58

change the value that change value will

play01:00

be assigned to the property of the

play01:02

component class let's try to understand

play01:05

two-way data binding with an example and

play01:07

how we can achieve

play01:09

it but before that let me go ahead and

play01:11

let me remove everything from here what

play01:13

I will do is I will simply comment all

play01:15

the stml content from here I'll simply

play01:19

keep it for your reference and from this

play01:21

lecture we are going to implement our

play01:23

actual e-commerce application and the

play01:25

first thing which we are going to do is

play01:27

we are going to implement the search

play01:28

functionality and and for that we need

play01:30

to implement a search textt box and for

play01:33

the search box let's go ahead and let's

play01:35

create a new component so I'll go ahead

play01:37

and I will open a new terminal and in

play01:40

here we want to create a new component

play01:42

inside this product list component so

play01:45

first we need to move to that product

play01:46

list component for that let's use CD

play01:48

command so from The Source folder we

play01:50

want to go to app folder and in the app

play01:53

folder we have product list folder let's

play01:57

press enter and now we are in the

play01:59

product list folder

play02:00

let me go ahead and let me clear the

play02:01

terminal first and now let's type the NG

play02:05

generate command and here we want to

play02:06

generate a new component and I'm going

play02:08

to call this component

play02:10

search let's press

play02:12

enter and now a search component should

play02:15

be created inside this product list

play02:17

component so if I expand this product

play02:19

list component there we have this search

play02:20

folder in there we have our search

play02:23

component now from this component again

play02:25

I will go ahead and I will delete this

play02:27

spec. TS file

play02:30

okay so here we have our search

play02:32

component class decorated with at

play02:33

component decorator the selector there

play02:35

is app search so I'll keep it like that

play02:38

and the template URL here is search

play02:40

component. HTML so let's go to that view

play02:43

template and in here we have a paragraph

play02:46

element but we don't need this paragraph

play02:47

element here instead we want to write

play02:49

some HTML in order to display a text box

play02:52

and a button for that in order to save

play02:55

some time I have already returned some

play02:57

HTML so I'll copy that HTML from here

play03:00

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

play03:02

it

play03:03

there we don't need these two divs for

play03:06

now so let's remove it so in this HTML

play03:09

we have this div inside that div we have

play03:11

an input element and we also have a

play03:13

button element then we have another div

play03:16

and inside this div we are simply

play03:18

displaying a

play03:20

paragraph okay so this is a very simple

play03:23

HTML here now to design this HTML I have

play03:27

also written some CSS so let's go ahead

play03:29

and let's grab that CSS let's grab that

play03:31

CSS from here and let's go to VSS code

play03:35

and let's go to the stylesheet of the

play03:37

search component which is this search

play03:38

component. CSS and let's paste it

play03:41

there and now let's go to search

play03:44

component.ts and let's use this selector

play03:48

in our product list component so here we

play03:51

have this product list component. HTML

play03:54

in there we have commented everything so

play03:56

the first thing which we want there is

play03:57

the search text box for that we can use

play04:00

the select of the search component with

play04:03

this if we go to the web

play04:05

page it will look something like this so

play04:08

here we have our input element and the

play04:10

button and here we have our

play04:12

paragraph okay now don't worry about

play04:15

this paragraph for now I will explain

play04:17

you why we have this paragraph here and

play04:19

now with the help of this text box let's

play04:22

go ahead and let's learn about two-way

play04:23

data binding two-way data binding is

play04:26

basically a combination of property

play04:28

binding and event binding so we have

play04:30

learned that using property binding we

play04:32

can bind data from the component class

play04:33

to view template and using event binding

play04:36

we can bind data from view template to

play04:38

component class so this two-way data

play04:40

binding is simply a combination of

play04:42

property binding and event binding let's

play04:45

see how we can achieve two-way data

play04:47

binding by combining property binding

play04:49

and event

play04:50

binding so here let me go ahead and let

play04:52

me close this product list component.

play04:54

HTML let's also close this product list

play04:57

component.ts then

play05:00

let's close this search component. CSS

play05:02

we don't need it anymore and we need to

play05:04

work with search component. HTML which

play05:06

is the view template and search

play05:08

component.ts there we have the search

play05:09

component class so here what we are

play05:12

going to do is we are going to create a

play05:15

property called search text it is going

play05:18

to be of type string and for now let's

play05:21

say search text is maybe men's VI so

play05:26

here what we want is we have this search

play05:29

text and in the view we have this search

play05:32

text box this input element so we want

play05:35

to prepopulate this input element with

play05:38

the search text which we have assigned

play05:40

to this sech text property in this case

play05:42

we want to pre-populate this input

play05:45

element with this value men's V for that

play05:48

we can use oneway data binding where we

play05:51

want to pass data from component class

play05:53

to view template and we have learned for

play05:55

that we can use property binding right

play05:57

so here in the search component. HTML on

play06:00

this input element we are going to bind

play06:03

the value property for that we need to

play06:05

wrap it within square brackets like this

play06:08

and to that we can assign any typescript

play06:10

expression Within These double codes

play06:12

here we simply want to assign this

play06:15

search text

play06:16

property to this value property and with

play06:20

this if we save the changes and if you

play06:22

go to the web

play06:23

page there you will notice that this

play06:26

text box is prepopulated with that value

play06:28

man's spere

play06:30

and here also search result for there

play06:32

also we want to show that same value the

play06:34

search text value So currently we are

play06:37

hardcoding it as you can see we are

play06:39

hardcoding it here but instead of

play06:41

hardcoding it let's go ahead and let's

play06:43

use our search text

play06:46

property okay if you save the changes

play06:48

again and if you go back to the web

play06:50

page now you can see that value has been

play06:52

rendered if I go ahead and if I change

play06:54

this value to something else maybe

play06:57

women's watch

play07:01

and if you go back to the web page now

play07:04

that value should be rendered in the

play07:05

text box as well as here so whatever we

play07:09

are going to search using this search

play07:11

text box that should be displayed here

play07:15

and also in the text box so this is

play07:17

oneway data binding where we are passing

play07:19

data we are binding data from the

play07:21

component class to view template now

play07:25

what we also want is whenever the user

play07:27

types something inside this search box

play07:30

for example man's shoe this value should

play07:35

be assigned back to this search text

play07:39

property okay and for that we have

play07:42

learned that we can use event binding so

play07:45

here when we will input something inside

play07:47

this text box an input event will happen

play07:51

we are going to bind that input event to

play07:54

this search text property so here let's

play07:56

go to search component. HTML and on this

play07:59

input element let's also bind input

play08:02

event okay and in order to bind an event

play08:07

we need to wrap it within parenthesis

play08:09

like

play08:10

this to this let's assign a function

play08:13

let's call it maybe update search

play08:19

text okay and in the last lecture we

play08:22

learned that whenever an event occurs it

play08:25

emits an event object and that event

play08:28

object is available inside the dollar

play08:31

event

play08:32

variable right so whenever this input

play08:35

event will happen on this input element

play08:38

it is going to emit an event object in

play08:41

this case it is going to emit input

play08:43

event object so that will be assigned to

play08:46

this dollar event variable and we are

play08:49

passing that event to this update search

play08:51

text method now here we have this error

play08:53

because we need to create this

play08:56

method so let's go ahead and let's

play08:58

create that method here

play09:00

here okay and it is also going to take a

play09:03

parameter and here we also need to

play09:06

specify the type of this event the type

play09:08

of the event which is going to be

play09:09

emitted for now let's set it to any but

play09:13

in the last lecture we learned that

play09:14

whenever the input event happens it

play09:17

raises an event of type input event

play09:20

right so here we can also specify that

play09:23

type which is input event and in the

play09:28

last lecture we also saw that on this

play09:30

input event we will have a Target

play09:33

property which basically stores the

play09:35

Target on which the event has happened

play09:38

in this case the event will occur on

play09:40

this input element so this target

play09:43

property is going to store the Dome

play09:45

object of that input element and on that

play09:48

we can access its value so this value it

play09:51

is going to store the value which the

play09:52

user has entered inside the text box

play09:54

inside the input element and now we want

play09:57

to assign that value to this search text

play09:59

property so here we can say this do

play10:03

search text equals whatever value the

play10:05

user has entered inside this input

play10:07

element now here we have this error

play10:10

which says event. Target is possibly

play10:12

null to fix this problem for now let's

play10:15

simply set the type as

play10:17

any okay we will talk about this error

play10:19

letter in this course but for now here

play10:21

we want to understand two-way data

play10:22

binding so for now let's simply set the

play10:25

type of this event to any and in that

play10:27

case that error is gone all right so in

play10:30

this way we are binding the value which

play10:33

the user is typing in the text box to

play10:35

this search text so whenever the value

play10:37

in the text box will change that new

play10:39

value will be assigned to this search

play10:41

text here so if I save the changes if we

play10:44

go back to the web

play10:45

page initial value is women's watch

play10:48

that's because that's what we have set

play10:50

the initial value for the search text

play10:53

now when we start typing something

play10:55

inside this text box let's say men's

play10:59

shoes you see that value is being

play11:02

rendered here that means that value is

play11:05

being assigned to the search text

play11:07

property and that is being rendered here

play11:10

so in this way by combining property

play11:13

binding and event binding we can achieve

play11:15

two-way data binding and that's why I

play11:17

said that two-way data binding is

play11:19

basically the combination of property

play11:22

binding and event

play11:23

binding now we can achieve this two-way

play11:26

data binding in a more simpler way

play11:29

so instead of using property binding and

play11:33

event binding like this we have another

play11:36

way and that is by using a built-in

play11:39

directive called NG model so again when

play11:43

we use NG model that time also since we

play11:46

have learned that two-way data binding

play11:47

is a combination of property binding and

play11:49

event binding what we do in property

play11:51

binding in property binding we use a set

play11:53

of square brackets and in there we

play11:56

specify the property name and in event

play11:58

binding we use a a set of parenthesis

play12:00

and in there we specify the event name

play12:03

so this is the syntax which we need to

play12:05

use and inside this parenthesis so first

play12:09

we have a set of square brackets inside

play12:11

that we have a set of parenthesis and in

play12:14

there we need to use a directive called

play12:16

NG

play12:18

model okay and to this we can go ahead

play12:21

and we can assign the property which we

play12:23

want to bind in this case we want to

play12:25

bind search text property now here you

play12:29

will see that we have an error and if I

play12:31

H this error it says can't bind to NG

play12:34

model since it isn't a known property of

play12:37

input now to resolve this problem all we

play12:39

have to do is let's first save the

play12:41

changes here and all we have to do is we

play12:45

have to go to app module. TS file there

play12:48

we need to import NG model so for that

play12:51

we can say import

play12:54

NG model and we want to import it from

play12:58

angular /

play12:59

forms okay we need to import NG model

play13:03

from angular / forms and now we need to

play13:07

register it now since it is a third

play13:09

party Library we are going to use this

play13:11

NG model we are not creating it right it

play13:13

is a third party Library so since it is

play13:16

a third party Library we are not going

play13:18

to specify it inside this declarations

play13:20

array we are going to specify it inside

play13:23

this Imports array so there let's go

play13:26

ahead and let's register this NG model

play13:30

and here we have an error and if I over

play13:32

over this error it says the directive NG

play13:35

model appears in import but it's not

play13:38

Standalone and cannot be imported

play13:39

directly that's because here instead of

play13:41

importing NG model we need to import

play13:45

form

play13:46

module and again we need to import form

play13:48

module from angular / forms and we need

play13:51

to register this forms

play13:53

module inside this

play13:56

Imports okay and with this all the

play13:58

errors ion okay so in order to use this

play14:03

NG

play14:04

model we need to import forms module

play14:08

from angular / forms and then we need to

play14:11

register it in the Imports array because

play14:14

it is a third party Library we are using

play14:16

so it should be registered within import

play14:17

Sur when we are creating a component or

play14:20

a directive of our own that we declare

play14:22

inside the Declarations array keep this

play14:25

point in mind all right with this let's

play14:26

save the changes let's close this

play14:29

module. TS let's go to search component.

play14:31

HTML and here also the error is gone and

play14:34

in this way using this NG model we can

play14:36

achieve two-way data minding so if you

play14:39

go to the web

play14:41

page here you see initially the value

play14:44

stored in the search text is women's

play14:46

watch so that has been rendered here now

play14:48

as soon as I start typing something here

play14:50

for example iPhone you will see that is

play14:53

being rendered here and also here so

play14:57

whatever value we are typing here that

play14:58

is getting assigned to the search text

play15:00

and then that value is being rendered

play15:02

here and this is what two-way data

play15:04

binding is in two-way data binding The

play15:07

Binding happens in both the directions

play15:09

the data is binded from component class

play15:11

to the view template and at the same

play15:13

time from The View template to the

play15:14

component class here this search text

play15:18

property it is binded with this input

play15:21

element so whenever the value of the

play15:23

search text property will change that

play15:25

will reflect in this input element and

play15:27

whenever the value inside this input

play15:29

element will change that will reflect in

play15:31

this search text property this is what

play15:34

two-way data binding is so this is all

play15:37

from this lecture if you have any

play15:39

questions then feel free to ask it thank

play15:41

you for listening and have a great day

Rate This

5.0 / 5 (0 votes)

Связанные теги
AngularData BindingComponentTwo-Way BindingEvent BindingString InterpolationProperty BindingSearch ComponentE-CommerceWeb Development
Вам нужно краткое изложение на английском?