I have app component with two child comoponent, namely first and second and I'm navigating through router outlet tag. I want to change parent component variable to 'XYZ' when I click on child component button.
app.component.html
<a routerLink = "/first">
First
</a>
<a routerLink = "/second">
Second
</a>
<div>
<router-outlet></router-outlet>
</div>
{{myvar}}
app.component.ts
export class AppComponent {
title = 'childParentInteraction';
myvar : String = "ABCD";
}
app-routing.module.ts
const routes: Routes = [
{
path:"first", component: FirstComponent
},
{
path:"second",component: SecondComponent
}
];
first.component.html
<div>
<p>first works!</p>
<button (click)="changeVar()">
Change variable
</button>
</div>
first.component.ts
export class FirstComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
changeVar(){
}
}