0

In a parent html, I have many components such as chart 1,2,3,4,5. Then whenever I changed the date, I want to send the selected date to all the components. The code below is the implementation of the parent child event emitter. But I am getting no value from this html file when I check <kt-chart1> component? Also, I want to know when I get the data so I can sort chart by date by executing functions.

html

<!-- ----------------------------------------------------------------------- -->
<!--                            date range field                             -->
<!-- ----------------------------------------------------------------------- -->
<div class="row">
    <div class="col-xl-4">
        <mat-form-field class="flex-container">
            <div [formGroup]="form">
                <input
                    matInput
                    placeholder="Choose a date"
                    [satDatepicker]="picker2"
                    formControlName="date"
                    (dateChange)="DateChanged($event)"
                />
            </div>
            <sat-datepicker #picker2 [rangeMode]="true"> </sat-datepicker>
            <sat-datepicker-toggle
                matSuffix
                [for]="picker2"
            ></sat-datepicker-toggle>
        </mat-form-field>
        <div class="col-xl-6 chartPadding">
        <h4 class="text-center">Cost Per Tonne</h4>
        <kt-chart1 (sendingDateEmitter)="receivedDate($event)"></kt-chart1>
        <kt-chart2 (sendingDateEmitter)="receivedDate($event)"></kt-chart2>
        <kt-chart3 (sendingDateEmitter)="receivedDate($event)"></kt-chart3>

    </div>
    </div>
</div>


Typescript

    @Output() sendDateEmitter = new EventEmitter<any>();

    DateChanged($event) {
        this.sendDateEmitter.emit($event.value);

    }

2 Answers 2

1

To each of the charts add

@Input() date: Date;

And pass it to charts

<kt-chart1 (sendingDateEmitter)="receivedDate($event)" [date]="form.get('date').value"></kt-chart1>
<kt-chart2 (sendingDateEmitter)="receivedDate($event)" [date]="form.get('date').value"></kt-chart2>

Make sure form is initialized before rendering, otherwise you might need to do something like [date]="form.get('date')?.value"

To detect date changes in chart use OnChanges

ngOnChanges(changes: SimpleChanges) {
  if (changes.date) {
    // Date has changed, do something with it.
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Then do I need to Output vaule with emitter? I want to do something with the data received. probably changing the date of the chart. then how do I know if I am getting the value or not
I added how to detect date change in your chart
it's property biding and then I don't need a event emitter at all. Is that right? what is the use of the question mark in form.get?
Yes, it is binding so remove Emitter entirely. If template renders before form is initialized it will throw something like cannot read property value of undefined because form.get('date') will be undefined. Read more about Optional chaining
0

Why don't you just declare a variable and bind it to Child's input parameter. Something like this:

Parent Component:

parentDate: Date;

DateChanged($event) {
  this.parentDate = $event.value);
}

Parent Component html:

<kt-chart1 [childDate]="parentDate"></kt-chart1>

Child Component

@Input() childDate: Date;

Angular will automatically take care of all the event emitter and passing of data you are taking to do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.