29
votes

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?

5
The currently provided answers are wrong. According to docs the input and data-bound properties are not the same concepts. E.g. here: Initialize the directive or component after Angular first displays the data-bound properties and sets the directive or component's input properties. - Sasuke Uchiha

5 Answers

36
votes

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

2
votes

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

1
votes

@Sasuke Uchiha, @Input and data-bound properties are indeed the same. Please read the line "Keep in mind that a directive's data-bound input properties are not set until after construction. If you need to initialize the directive based on those properties, set them when ngOnInit() runs" at https://angular.io/guide/lifecycle-hooks

PS: Cant comment on the answer, so replying this way

0
votes

Reading the Docs, we note:

ngOnChanges():

"respond when Angular sets or resets data-bound input properties"

it will not be called if your component

"has no inputs or you use it without providing any inputs"

On ngOnInit method, we can also see that ngOnChanges() is not called when

"there are no template-bound inputs"

Finally, ngOnInit():

"initialize the directive or component after Angular first displays the data-bound properties and sets the directive or component's input properties."

Suming up, data-bound and input properties and template-bound inputs are all just an alias for the same thing, or as @GünterZöchbauer points, they are just "inputs".

-1
votes

data-bound properties are just properties of the class