Assign value to a date in angular 2 (binding)

1

Good evening.

I need to set / assign a value to a date of a specific component that is a date before loading my page. In this example I try to assign the first and last day of the month.

HTML:

<pf-input-date [(date)]="fechaHasta" mode="day" name="fechaHasta"></pf-input-date>

TypeScript:

export class LiquidoProductoComponent implements OnInit {
    fechaDesde: Date;
    fechaHasta: Date;
    date: Date;


    ngOnInit() {
        this.date = new Date();

        // Verificar
        let desde = new Date(this.date.getFullYear(), this.date.getMonth(), 1);
        var hasta = new Date(this.date.getFullYear(), this.date.getMonth() + 1, 0);

 // Esta asignacion la realiza pero no asi el binding

        this.fechaDesde = desde; 
        this.fechaHasta = hasta;

    };
}

Thank you very much already.

    
asked by pablOOO5 31.07.2017 в 23:58
source

1 answer

1

To achieve data-binding in two directions you need an event emitter with a "@Output" decorator for each variable that you want to associate with your template, and with the appropriate name according to the variable to which you make the data-binding using the suffix "Change".

In your example, you would need to add this code to your component:

@Output() fechaDesdeChange = EventEmitter<Date>();
@Output() fechaHastaChange = EventEmitter<Date>();

The suffix "Change" is by agreement, and therefore mandatory.

More details on the next page (in English): Angular 2 Training - Using Two-Way Data Binding

    
answered by 14.08.2017 в 21:02