What is data-bound properties?

@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


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


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


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

Tags:

Angular