简体   繁体   中英

How to change parent variable from child component in angular2

my parent component html:

<app-calendar-component [view]="calView" [callback]="calendarOptions" ></app-calendar-component>
<app-weekdetails-component *ngIf="test"></app-weekdetails-component>

my parent component ts:

public test:boolean=false;

appCalendarComponent:

ngOnInit(){
Calling services here and using data, after this process I need to set 
my variable test to be true which is defined in parent component.
}

So I need to set variable test to be true when 1st child(appCalendarComponent) is initialized.

You should use output way to comunicate with the parent component.

On the child:

import { Component, Output, EventEmitter } from '@angular/core';

@Output() componentInit = new EventEmitter<>();

ngOnInit(){
Calling services here and using data, after this process I need to set 
my variable test to be true which is defined in parent component.

//After init
 this.componentInit.emit();
}

On the parent:

HTML:

<app-calendar-component 
                  (componentInit)="componentInitialize()" 
                  [view]="calView" 
                  [callback]="calendarOptions">
</app-calendar-component>

TypeScript:

componentInitialize():void{
 this.test = true;
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM