What is data-bound properties?

后端 未结 2 1390
[愿得一人]
[愿得一人] 2020-12-06 09:51

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

Description

Implement this interface to execute cu

相关标签:
2条回答
  • 2020-12-06 10:04

    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

    0 讨论(0)
  • 2020-12-06 10:19

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

    0 讨论(0)
提交回复
热议问题