Search results

Custom event Emitter in Angular Calendar component

The two-way binding in Calendar 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 by using @output directive.

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

@Component({
    selector: 'app-root',
    template: `
  <div class="parentelement">
  <span><h4>Parent Component</h4></span>
   <div class="datevalue">
   <ejs-calendar id="calendar" #calendar (change)="deposit()" [value]="value" width="200px"></ejs-calendar>
   </div>
   </div>
   <child [xvalue]="value" (valueChange)="valuecheck($event)"> </child>
  `,
})
export class ParentComponent {
    @ViewChild('calendar')
    public calendar: CalendarComponent;
    value: Date;
    constructor() {
        this.value = new Date("2/1/2020");
    }
    deposit() {
        this.value = this.calendar.value;
    }
    valuecheck(args: any) {
        this.value = args;
    }
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
// import the CalendarModule for the Calendar component
import { CalendarModule } 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, CalendarModule ],
  declarations: [ ParentComponent,  ChildComponent ],
  bootstrap:    [ ParentComponent]
})
export class AppModule { }
import {Component, EventEmitter, Input, Output, ViewChild} from '@angular/core'
import { CalendarComponent } from '@syncfusion/ej2-angular-calendars';

@Component({
  selector: 'child',
  template: `
  <br>
  <span><h4>Child Component</h4></span>
  <div class="childelement"> 
    <ejs-calendar id="date2" #child (change)="ValueChange()" [value]="xvalue" width="200px"></ejs-calendar>
  </div>
  `,
})
export class ChildComponent {
  @ViewChild('child')
  public DateValue: CalendarComponent;
  @Input() xvalue: Date;
  @Output() valueChange = new EventEmitter();

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

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);
#container {
    visibility: hidden;
}

#loader {
  color: #008cff;
  height: 40px;
  width: 30%;
  position: absolute;
  top: 45%;
  left: 45%;
}

#element {
    display: block;
    height: 350px;
}

#wrapper {
    width: 598px;
    margin: 0 auto;
    height: 250px;
}

child, .parentelement{
    display:inline-block;
    text-align: center;
}
child{
    margin-left:35px;
}
h4{
    margin:0 0 5px 0;
}