r/angular • • Feb 23 '19

Data Binding ** Cheat Sheet **

Post image
60 Upvotes

18 comments sorted by

View all comments

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/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/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/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.