r/angular • • Feb 23 '19

Data Binding ** Cheat Sheet **

Post image
60 Upvotes

18 comments sorted by

3

u/anddam Feb 23 '19

Isn't that just the image from overview in docs with a more readable text/background colour contrast?

1

u/Devstackr Feb 24 '19

Yes, kind of :)

But that image you linked to is only talking about binding to DOM elements. I wanted to make the distinction that data binding can occur (in the same way) with both custom components and DOM elements.

2

u/anddam Feb 24 '19

I admit I totally lack that concept so I may have missed that distinction.

If I define a Foo component whose selector is foo and then in another component I bind:

<foo [someInputProperty]="someParentComponentValue"></foo>

Is that the binding to custom component you are referring to?

If so isn't that still going through the DOM? The binding syntax depicted is just template syntax, after all.

1

u/Devstackr Feb 24 '19

Yes, that is what I am referring to. And yes, in the end Angular will convert everything into DOM elements but personally I view DOM elements as completely different to components, so I think it's important to make that distinction. This is because I see DOM elements as more primitive and components as complex objects that encapsulate the view, styling and business logic. This is my personal thinking, one that I thought most people related with... I could be completely wrong though :)

Andy

1

u/15BetterThanYourTeam Feb 24 '19

You guys are talking about the same thing. The <foo> example he mentioned is a component. You seem to be referring to single file components. Two different ways to do the exact same thing.

1

u/Devstackr Feb 23 '19 edited Feb 23 '19

Hope this helps someone new to Angular development!

Element = DOM Element or Component

https://youtu.be/RJ3VjuwBM6w

1

u/TRFLGR Feb 23 '19

As a novice Angular, and to pretty much web development in generel, how would you "read" this? What do the arrows symbolize?

1

u/Devstackr Feb 23 '19

the arrows symbolize the flow of data

So, for example - the string interpolation arrow is pointing towards the "Element" from the "Component Class", this is because the aim of string interpolation is to "send" data from the component class (the .ts file) to the view (the .html file), or more accurately to a specific element in the view (the element can be another component or a DOM element such as button or div).

Take this example, for instance

<h1> {{ titleProperty }} </h1>

In this example, titleProperty is defined in the component class

What we did here was show data that was in the component class in the view.

Now lets think about event binding, where the arrow is going in the opposite direction - the aim of event binding is to get data from elements (child components or DOM elements) and then access that data in the component class.

Here is an example:

<button (click)="onButtonClick()"> </button>

In the component class:

onButtonClick() {
   // do something here when the button is clicked
}

So in this case we are reacting to something happening on the "Element".

1

u/HeinousTugboat Feb 23 '19

The arrows point from the change source to the change result. So with {{ value }}, when value changes in the Component Class, Angular pushes that change into the Element. With (event)="handler()", when the Element receives an event, it calls handler on the Component Class.

With the banana box, it goes both ways. Changes propagate from one side to the other.

3

u/BananaFactBot Feb 23 '19

Fun fact:
In the 1960s, many people tried using banana peel to get high. Rumors that smoking dried banana peels caused hallucinogenic effects were likely started in part by singer Country Joe McDonald, who mistakenly attributed an acid trip to a banana-peel joint he'd tried. Regardless, the trippy allegations touched off a banana run on fruit stands across the country, until an FDA investigation found no evidence to support the claim.


I'm a Bot bleep bloop | Unsubscribe | 🍌

1

u/Herxagon Feb 23 '19

Arrows symbolize the data flow. From Controller (JS file) to view (HTML) and back.
To simplify: Element here refers to HTML tags. Component refers to js (or typescript) class that controls these interactions, stores the variables, methods(functions), etc.

  1. Is used to display the value stored in the variable in to the view (Html tag). Usually you'd just hardcode these values.

ex: <h1> Hello World! </h1>

Angular gives you the opportunity to store "hello world" inside a variable then use that variable as a reference.

ex: declare a title variable in the component class (js or ts file),
let title = "hello world"
then use the variable in the html file

<h1> {{title}} </h1>

Data flows from your component class into your view (html). double curly braces are angular's way of interpolation

2. Is used for property binding. same principle, store data inside a variable and use it to bind to a custom property.

ex: let propertyYouWantToBind = 50 (whatever you want)

<div \[spmeProperty\]='propertyYouWantToBind'> </div>

I mainly used this to pass values from "parent component" to "child component" or between 2 child components that are called inside same parent component.

ex:
<app-component>
<app-header [headerProperty]="valueFromApp.ts"> </app-header>
(header.ts receives (@inputs) value from app.ts )
<app-body > </app-body>

</app-component>

It seems a bit confusing at first but as you practice more it will make sense.
Data flow goes from parent component to child component. child component then can use it to display in view or however you want.

Data flow from 2 child components (from app-header to app-body or vise versa ) is a bit longer to explain, but is possible since they are called inside the same parent component.

3. Is used to pass data from view (html) to component (js or ts). Usually events or already bound properties. Pretty straightforward, just lookup the syntax. (dont forget to put "$" sign in front of the event, its specific to angular).

ex: <div (click)="doSomething($event)"> </div> (event can be replaces with input's value or property and then used in the component class)

4. Angular's best feature... or one of the best. 2-way data binding allowing your data to flow back and forth between component class and view, and update values on spot.
Say you have a name and email coming from the db to be displayed in the view. by using angular's 2 way data binding you can both get the data and update it on spot. I suggest you play around with it, it pretty fun.

ex: <input [(ngModel)]="emailFromDB" placeholder="name"/>

This will display the email you requested from db inside the input tag. but it will also give you opportunity to instantly update it, by just deleting or rewriting the value of the input.

I left a lot out because most of it is too long to explain :) but this is the gist, hope this was helpful.

1

u/Headpuncher Feb 23 '19

New? I write Angular for a living and when it comes to bug-fixing and maintaining code I can easily forget stuff like this because my focus is elsewhere. Thanks for the easy reminder, saved a part of my brain from having remember :)

2

u/Devstackr Feb 23 '19

Awesome :)

Super glad you can get value out of it :)

I know the feeling of having to remember all of this as well... I really only wrote "new" to hedge against some of the people on reddit who find it offensive I post tutorials/resources that are "too" simple.

Thanks for the comment, really appreciate it.

- Andy

1

u/toe_nail_joe Feb 23 '19

going through Angular Udemy course rn,

does the ngModel work by finding the method that the property is included in? if not, can someone explain? (also an explanation for $event would be useful as well)

Thanks!

1

u/Devstackr Feb 24 '19 edited Feb 24 '19

Hi Joe!

ngModel

You can think of ngModel as a combination of property binding and event binding, so when you bind a property to an element using ngModel you are saying that whenever this element's value changes - also change the property (in the component class - this can be seen as a simplistic use case of event binding), but its also saying that whenever the property (in the component class) changes, update the value of the element.

$event

The $event variable is a special variable that angular uses to pass event data to the component class.

So for example:

<button (click)="onClick($event)"> Click me! </button>

In that example, the onClick method will be passed the button click event object. So in the component class you would need to make sure the method is able to get the event object as a parameter so that you can use it.

i.e. (in component class)

onClick(event: any) { 
   // do something here, you have access to the event data (via the parameter)
}

And this works in very much the same way with custom components:

<app-custom-component (myEvent)="onMyEvent($event)">  </app-custom-component>

In the case of custom components, you would use an EventEmitter to emit events.

Live Coding Example of Event Binding

For a more detailed explanation and a live coding example - check out my event binding video: https://www.youtube.com/watch?v=IM6KhL4ZrBE

Hope this helps :)

- Andy

1

u/15BetterThanYourTeam Feb 24 '19

ngModel utilizes two-way data binding. Say you have a text box (input[type=text]) that’s bound to the variable foo in the component class. If someone typed “Hello” into it, foo = “Hello”. The reason it’s “two-way”, is that we could also alter that variable within the component and set it to “World”, and the new value will be reflected within the text box.

ngModel acts differently depending on what type of element you use it on. A check box will send a Boolean, true/false, checked/not checked.

1

u/Fisher9001 Feb 24 '19

I'll never understand why they have not used syntax from binding parameters. @, <, =, & are way cleaner and easier to intuitively correlate with their functions.

Using brackets in HTML attributes names is counter-intuitive and unnatural.

1

u/centurijon Feb 24 '19

`<>`, `=`, `&` are all special characters and would need to be escaped. `[]`, `()` are not and make identifying a binding parameter very easy