Search code examples
javascriptangulardata-bindingangular6

how to pass dynamic data from parent to child in angular2+


As we know , in angular parent to child communication happened using @Input decorator but how to pass dynamic data from parent to child.for example 'name' property is defined in parent and it's value changes dynamically and updated value of 'name' we are going to using in child component so how to achieve it without services.


Solution

  • you can watch your input changes in the child component with ngOnChanges lifecycle

    in your child component you should implements OnChanges

    ngOnChanges(changes: SimpleChanges): void {
        for (let propName in changes) {
          let change = changes[propName];
          if (propName === "YOUR_INPUT_NAME") {
            this.YOUR_INPUT_NAME = (change.currentValue);
          }
        }
      }