Search results

Custom event Emitter

The two-way binding in DateRangePicker can also be achieved using the custom event binding and property binding in the controls present in two different components. To create custom event, we need to create an instance of event emitter.

In the following example, property binding is used to share the data from the parent component to the child component using @input directive and custom event binding is used to share the data from the child component to the parent component by using @output directive.

Source
Preview
app.component.ts
app.module.ts
child.component.ts
main.ts
import { Component, ViewChild } from '@angular/core';
import { DateRangePickerComponent } from '@syncfusion/ej2-angular-calendars';

@Component({
    selector: 'app-root',
    template: `
  <div class="parentelement">
   <div class="rangevalue">
   <ejs-daterangepicker id="daterangepicker" #range (change)="deposit()" [value]="value" placeholder="Parent component" floatLabelType="Always" width="200px"></ejs-daterangepicker>
   </div>
   </div>
   <child [xvalue]="value" (valueChange)="valuecheck($event)"> </child>
  `,
})
export class ParentComponent {
    @ViewChild('range')
    public DateRange: DateRangePickerComponent;
    value: Date[];
    constructor() {
        this.value = [new Date("1/1/2019"), new Date("2/1/2020")]
    }
    deposit() {
        this.value = this.DateRange.value;
    }
    valuecheck(args: any) {
        this.value = args;
    }
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
// import the DateRangePickerModule for the DateRangPicker component
import { DateRangePickerModule } from '@syncfusion/ej2-angular-calendars';
import { ParentComponent }  from './app.component';
import { ChildComponent }  from './child.component';

@NgModule({
  //declaration of ej2-angular-calendars module into NgModule
  imports:      [ BrowserModule, DateRangePickerModule ],
  declarations: [ ParentComponent,  ChildComponent ],
  bootstrap:    [ ParentComponent]
})
export class AppModule { }
import {Component, EventEmitter, Input, Output, ViewChild} from '@angular/core'
import { DateRangePickerComponent } from '@syncfusion/ej2-angular-calendars';

@Component({
  selector: 'child',
  template: `
  <br>
  <div class="childelement">
     <ejs-daterangepicker id="daterange2" #child (change)="ValueChange()" [value]="xvalue" placeholder="Child component" floatLabelType="Always" width="200px"></ejs-daterangepicker>
  </div>
  `,
})
export class ChildComponent {
  @ViewChild('child')
  public DateRangeValue: DateRangePickerComponent;
  @Input() xvalue: Date[];
  @Output() valueChange = new EventEmitter();

  ValueChange() {
  this.xvalue = this.DateRangeValue.value;
  this.valueChange.emit(this.DateRangeValue.value);
  }
}
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);