33

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?

JayChase
  • 11,174
  • 2
  • 43
  • 52
softshipper
  • 32,463
  • 51
  • 192
  • 400
  • The currently provided answers are wrong. According to [docs](https://angular.io/guide/lifecycle-hooks) 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 Jun 18 '20 at 12:46

7 Answers7

38

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

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 2
    This does not answer the actual question: What is the definition of data-bound properties? – Anton Aug 01 '17 at 06:22
  • 1
    The HTML code wasn't visible because it wasn't indented enough. The answer was accepted, so I think it actually did answer the question ;-). Thanks for the hint anyway - I updated it. – Günter Zöchbauer Aug 01 '17 at 06:28
  • @GünterZöchbauer from your answer, I understand component properties which have `@Input`, called as `data-bound` property, correct me if I am wrong. – Hari Oct 05 '17 at 18:20
  • I don't know about `data-bound`, for me they are just "inputs". From the user of the components view (parent component), they are properties, and you can use data-binding to bind values to them. You can use data-binding also to attributes and native properties. Therefore I don't know if calling it `data-bound property` makes any sense. – Günter Zöchbauer Oct 05 '17 at 18:22
  • 1
    @GünterZöchbauer, actual question is "What is Data-bound property?" which you didn't answered. – TheTom Oct 11 '17 at 17:15
  • @TheTom not sure what you try to tell me with your comment. "Data-bound property" is probably a just made up word. I can't remember seeing it being mentioned in relation to Angular. – Günter Zöchbauer Oct 11 '17 at 17:22
  • @TheTom also the zero_coding accepted the answer, therefore I'd say you can assume my answer answers the question. – Günter Zöchbauer Oct 11 '17 at 18:33
  • So this means we can only use ngModel on properties that are fed in using Input()? That sounds wrong to me since I see many examples that don't do that quoting https://angular.io/guide/template-syntax#ngmodel---two-way-binding-to-form-elements-with-ngmodel "The [(ngModel)] syntax can only set a data-bound property. If you need to do something more or something different, you can write the expanded form." – Sammaye Jun 13 '19 at 20:39
  • @Sammaye I don't understand your question. I do not mention `ngModel` anywhere above and I don't understand what you mean with quoting. `ngModel` is for components that provide a `VontrolValueAccessor`, not for Inputs/Outputs.`[(...)]` is for Input/Output combinations that follow specific naming conventions. `[...]` is for Inputs and properties, `(...)` for Outputs and DOM events. – Günter Zöchbauer Jun 14 '19 at 03:06
  • Ok,I think I just misunderstood, so NgOnModelChange can only happen to data bound properties but two way binding of NgModel can happen to any property, that's what I was confusing (I think, though that documentation page seems to contradict further reading I did) – Sammaye Jun 16 '19 at 23:55
  • `(NgModelChange)` is just one direction of the data binding. `[(NgModel)]` is a short form for both directions - the combination of `[NgModel]` and `(NgModelChange)`. You can use this short form of data binding for every combination of `@Output() somePropNameChange = ...; @Input() somePropName;`. In this case it would be `[(somePropName)]`. `NgModel` is only for input elements where a ControlValueAccessor` is provided for (by default `` and other HTML input elements, but can also be implemented for custom components) – Günter Zöchbauer Jun 17 '19 at 03:12
  • @GünterZöchbauer, the `data-bound property` is not a made up word. Please, refer to official sources before posting your answers. The `data-bound property` is used extensively in the Angular official docs. E.g. [here](https://angular.io/guide/lifecycle-hooks). And since your answer does not give a definition for the `data-bound property` it makes the whole thing of posting questions on SO confusing, since now I would have to post the exact same question you have answered here. – Sasuke Uchiha Jun 18 '20 at 12:36
  • The answer is wrong. Input properties and data-bound properties are not the same in Angular. – Sasuke Uchiha Jun 18 '20 at 12:47
  • Actually data-bound property and `@Input()` properties are the same. If you know better post a better answer. – Günter Zöchbauer Jun 18 '20 at 12:54
2

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

user8307736
  • 225
  • 2
  • 6
2

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

2

The question "What are data-bound properties" is indeed relevant.

A data-bound property, as @Bhargav Rao points out, can be any property / field of the component's class whose value is passed to the template, regardless of "how it looks like".

A data bound property can be:

  • A field of the class that is passed to an attribute of an HTML element, e.g. isUnchanged => [disabled]="isUnchanged"

  • A field of the class that is passed to an ngModel, e.gl name => [ngModel]="name"

  • A field of the class that happens to be an input property (i.e., it is preceded by an @Input decorator)

Possibly also, an output property (not sure about this last one).

BTW, Angular docs fells short of clarity and preciseness on this subject.

Hope this helps.

1

@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

From Angular Glossary:

In data binding, you declare the relationship between an HTML widget and a data source and let the framework handle the details. 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
  • Attribute binding
  • Class and style binding
  • Two-way data binding with ngModel

To bind a property you enclose it in square brackets, [], which identifies the property as a target property. Target property may be name of an attribute, an input property of a component/directive. The brackets are needed if right-hand side of assignment is wanted to be a dynamic expression. Without them, Angular treats the right-hand side as a string literal and sets the property to that static value.

-1

data-bound properties are just properties of the class

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140