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?
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
@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.
@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" .
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:
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.
数据绑定属性只是类的属性
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.