Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 有没有办法使用NgbDatepicker';是否在组件上使用navigateTo(),而不是在ngAfterViewInit()上使用它?_Angular_Datepicker_Ngb Datepicker - Fatal编程技术网

Angular 有没有办法使用NgbDatepicker';是否在组件上使用navigateTo(),而不是在ngAfterViewInit()上使用它?

Angular 有没有办法使用NgbDatepicker';是否在组件上使用navigateTo(),而不是在ngAfterViewInit()上使用它?,angular,datepicker,ngb-datepicker,Angular,Datepicker,Ngb Datepicker,我正在使用NgbDatepicker <ngb-datepicker #dp [(ngModel)]="scheduledStartDate" [ngModelOptions]="{standalone:true}"> </ngb-datepicker> 但是有没有一种方法可以在其他函数上使用navigateTo()。在初始化之前,您不能使用NgbDatepicker。为了确保它可用,我们有ngAfterViewInithook。

我正在使用
NgbDatepicker

<ngb-datepicker #dp [(ngModel)]="scheduledStartDate" [ngModelOptions]="{standalone:true}">
</ngb-datepicker>

但是有没有一种方法可以在其他函数上使用
navigateTo()

在初始化之前,您不能使用
NgbDatepicker
。为了确保它可用,我们有
ngAfterViewInit
hook。因此,如果您想进行一些初始化,可以在
ngAfterViewInit
中进行。但是如果您仍然想从
视图初始化之前调用的某个函数中使用它(比如构造函数,因为您没有告诉我们从何处调用它),您可以使用
setTimeout
作为解决方法,但我不建议这样做。例如,请参见下面的组件-

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html'
})
export class AppComponent implements AfterViewInit {

  @ViewChild('dp') datepicker: NgbDatepicker;
  scheduledStartDate: NgbDateStruct = {year: 2222, month: 10, day: 10};
constructor(private calendar: NgbCalendar) {
  console.log(this.datepicker? 'NgDatePicker is available': 'NgDatePicker not initialized');
  setTimeout(() => {
    this.datepicker.navigateTo({year:2090, month: 10});
  }, 1000);
}
ngAfterViewInit() {
    console.log(this.datepicker? 'NgDatePicker is available': 'NgDatePicker not initialized');
}
}
请检查工作正常的示例。
app.component.html
有两个按钮可以导航到两个不同的年份

更新

您也可以在
-

<div *ngIf="addEditForm">
    <ngb-datepicker #dp [(ngModel)]="scheduledStartDate"  [startDate]="scheduledStartDate" (navigate)="date = $event.next" *ngIf="addEditForm"></ngb-datepicker>
 </div>

请参阅了解更多信息。

如果要设置任何特定日期,请:

以及使用按钮事件:

至当月

至2021年2月

和在组件中-

从'@ng bootstrap/ng bootstrap'导入{NgbDateStruct,NgbCalendar}
导出类NgbdDatepickerBasic{

model:NgbDateStruct;
日期:{年:数,月:数};

constructor(私有日历:NgbCalendar){}

selectToday(){this.model=this.calendar.getToday();}
}


如果没有ngAfterViewInit()

您可以在确定视图已初始化后以编程方式使用它…您想做什么?我想让日期选择器打开特定的月份。如何以编程方式使用它@WillAlexander视图初始化后(因此您在
nAfterViewInit
中使用它),您可以从任何地方调用日期选择器的
navigateTo
方法。不,它会给出一个错误:
错误类型错误:无法读取未定义的属性“navigateTo”
,这意味着视图未初始化,这是我的观点。在我的情况下,在我使用的函数上,navigateTo按钮打开表单。我对你的stackblitz做了一些调整,以适合我的例子。您可以在那里看到错误。@Neha可以在中看到
openAddForm()
。元素在
dom
中可用需要一段时间才能使用。导航到
仍然会出现错误,但是
[startDate]
这对我来说很有效。谢谢@Akash
<div *ngIf="addEditForm">
    <ngb-datepicker #dp [(ngModel)]="scheduledStartDate"  [startDate]="scheduledStartDate" (navigate)="date = $event.next" *ngIf="addEditForm"></ngb-datepicker>
 </div>
openAddForm() {
    this.addEditForm = true;
    this.scheduledStartDate = {year: 5555, month: 10, day: 10};
  }