Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 ngb-datepicker navigateTo

Tags:

angular

I am using ngb-datepicker in my application. I want to navigate to a particular month/year when a function is executed in my component class. I know there is method in ngbDatePicker called navigateTo that can called to navigate to particular month/year. But this is done in HTML page as below. Can I call the same method navigateTo in component class? Thank you.

<ngb-datepicker #dp [(ngModel)]="model" (navigate)="date = 
$event.next"></ngb-datepicker>
<button class="btn btn-sm btn-outline-primary mr-2" 
(click)="dp.navigateTo()">To current month</button>
like image 574
Rahul B Avatar asked Sep 08 '26 13:09

Rahul B


1 Answers

you can do it easily in component.ts

 @ViewChild('dp') datepicker: NgbDatepicker;

then ,

    ngAfterViewInit() {
    this.datepicker.navigateTo(..);
 }

in template ,

 <ngb-datepicker #dp ></ngb-datepicker>

note: you need to implement the component using AfterViewInit interface. and import NgbDatepicker, ViewChild and AfterViewInit to the component.ts

like image 84
Hiran Avatar answered Sep 11 '26 06:09

Hiran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!