I am working in Angular ,where -
I am trying to Update value in the child component , on value changes in the parent component
(as value is coming dynamically to the parent component from some other component) .
How I tried
I tried to pass data from parent component to child component Using @Input decorator
using @Input value is passed just once when component loads and latter on value is not passed
I am sharing my code below
Parent component
.html
<app-banner [tournamentType]='tournamentType'></app-banner>
.ts
child component
.ts file
import { Component, OnInit , Input } from '@angular/core';
import { ServicesService } from '../service/services.service';
@Component({
selector: 'app-banner',
templateUrl: './banner.component.html',
styleUrls: ['./banner.component.scss']
})
export class BannerComponent implements OnInit {
@Input() tournamentType;
sportsType : any = 1;
constructor(private rest : ServicesService) { }
ngOnInit() {
console.log("this. is banner page" + this.tournamentType);
alert('hello');
this.loadDataFromApi(1);
}
loadDataFromApi(sportsType) {
this.rest.getbanner(this.sportsType).then(res => {
console.log('>>>$$$$$ banner >>>>>> $$$$$$$$$$');
console.log(' @Input tournamentType; ====' + this.tournamentType );
console.log(res);
})
console.log(sportsType);
}
}
To update the parent state from the children component, either we can use additional dependencies like Redux or we can use this simple method of passing the state of the parent to the children and handling it accordingly.
To pass data from child to parent component in React:Pass a function as a prop to the Child component. Call the function in the Child component and pass the data as arguments. Access the data in the function in the Parent .
@Input() and @Output() give a child component a way to communicate with its parent component. @Input() lets a parent component update data in the child component. Conversely, @Output() lets the child send data to a parent component.
We can get child component values in the parent component by creating a reference to the child component using the @ref directive in the Parent component. Using the reference instance, you can access the child component values in the parent.
Values changes from parent to child components are reflected immediately. However, you can listen for value changes event in the child component. Read more about ngOnChanges
Here is an example on stackblitz
app.component.html
<hello name="{{ name }}"></hello>
<p>
Start editing to see some magic happen :)
</p>
<app-child [data]="count"></app-child>
app.component.ts
import { Component, OnInit } from "@angular/core";
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"],
})
export class AppComponent implements OnInit {
name = "Angular";
count = 0;
constructor() {}
ngOnInit(): void {
setInterval(() => this.updateCount(), 1000);
}
updateCount(): void {
this.count++;
}
}
child.component.html
<p>{{data}}</p>
child.component.ts
import { Component, OnInit, OnChanges, Input, SimpleChanges } from "@angular/core";
@Component({
selector: "app-child",
templateUrl: "./child.component.html",
styleUrls: ["./child.component.css"],
})
export class ChildComponent implements OnInit, OnChanges {
@Input() data: any;
constructor() {}
ngOnInit() {}
ngOnChanges(changes: SimpleChanges): void {
console.log("value changed", this.data);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With