Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is data-bound properties?

Tags:

angular

I am trying to understand OnInit functionality in angular2 and read the documentation:

Description

Implement this interface to execute custom initialization logic after your directive's data-bound properties have been initialized.

ngOnInit is called right after the directive's data-bound properties have been checked for the first time, and before any of its children have been checked. It is invoked only once when the directive is instantiated.

I do not understand directive's data-bound properties what does it mean?

like image 252
softshipper Avatar asked Sep 07 '16 10:09

softshipper


People also ask

What is a data bound property?

Data binding is an alternative to manually pushing application data values into HTML, attaching event listeners, pulling changed values from the screen, and updating application data values. Forms of data binding include: Interpolation. Property binding. Event binding.

What is data bound in Angular?

Data binding in AngularJS is the synchronization between the model and the view. When data in the model changes, the view reflects the change, and when data in the view changes, the model is updated as well.

What is Property binding in Angular?

Property binding in Angular helps you set values for properties of HTML elements or directives. Use property binding to do things such as toggle button functionality, set paths programmatically, and share values between components.

What is Property binding and event binding in Angular?

Property binding is the same thing as interpolation, except you can set the properties and attributes of various HTML elements. Event binding allows you to define events that occur in the template (user-initiated), and communicate to the component class. We'll take a look at each of these in this lesson.


2 Answers

When you have a component

@Component({   selector: 'my-component' }) class MyComponent {   @Input() name:string;    ngOnChanges(changes) {   }    ngOnInit() {   } } 

you can use it like

<my-component [name]="somePropInParent"></my-component> 

This make name a data-bound property.

When the value of somePropInParent was changed, Angulars change detection updates name and calls ngOnChanges()

After ngOnChanges() was called the first time, ngOnInit() is called once, to indicate that initial bindings ([name]="somePropInParent") were resolved and applied.

For more details see https://angular.io/docs/ts/latest/cookbook/component-communication.html

like image 65
Günter Zöchbauer Avatar answered Oct 04 '22 12:10

Günter Zöchbauer


@Input is a decorator that makes a class field as an input property and supplies configuration metadata. The input property is bound to a DOM property in the template. During change detection, Angular automatically updates the data property with the DOM property's value.

I hope this answer may help to understand this concept.

Above example contains name as an input which is bound as property for component in DOM structure & angular updates it based on changes.

like image 24
user8307736 Avatar answered Oct 04 '22 13:10

user8307736