NativeScript / Angular Page.getViewById returns 'undefined'
NativeScript / Angular Page.getViewById returns 'undefined'
我正在开发 NativeScript/Angular 应用程序。我是 NativeScript 的新手,正在学习基于早期版本的教程(我的是 4.1.0,不确定他们使用的是什么)。有一个带有 DatePicker 元素的模式对话框。我想把它 return 选定的日期传给父元素。该应用程序可以正常编译和启动。但是当我尝试打开模式时,出现错误 "Cannot set property 'year' of undefined"。
HTML如下:
<StackLayout class="modal-view-style">
<StackLayout>
<DatePicker id="datePicker"></DatePicker>
</StackLayout>
<Button class="btn btn-primary btn-active" text="submit" (tap)="submit()"></Button>
</StackLayout>
这是 class:
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { ModalDialogParams } from 'nativescript-angular/modal-dialog';
import { DatePicker } from 'tns-core-modules/ui/date-picker';
import { Page } from 'tns-core-modules/ui/page';
@Component({
moduleId: module.id,
templateUrl: './date-picker-modal.component.html'
})
export class DatePickerModalComponent implements OnInit {
constructor(private params: ModalDialogParams, private page: Page) { }
ngOnInit() {
let datePicker: DatePicker = <DatePicker>this.page.getViewById<DatePicker>('datePicker');
let currentdate: Date = new Date();
datePicker.year = currentdate.getFullYear();
datePicker.month = currentdate.getMonth() + 1;
datePicker.day = currentdate.getDate();
datePicker.minDate = currentdate;
datePicker.maxDate = new Date(2045, 4, 12);
}
public submit() {
let datePicker: DatePicker = <DatePicker>this.page.getViewById<DatePicker>('datePicker');
let selectedDate = datePicker.date;
let reserveTime = new Date(
selectedDate.getFullYear(),
selectedDate.getMonth(),
selectedDate.getDate()
));
this.params.closeCallback(reserveTime.toISOString());
}
}
有人知道这是怎么回事吗?我假设只是 api 发生了变化,但我无法在文档中找到它。如果我是对的,将 link 添加到文档中的适当位置将不胜感激。此外,如果这不是执行此类操作的 "preferred" 方法,我很想知道什么是。谢谢。
如果您将 NativeScript 与 Angular 一起使用,则使用 Angular 模板变量语法,如下所示:
<StackLayout class="modal-view-style">
<StackLayout>
<!-- notice the # -->
<DatePicker #datePicker></DatePicker>
</StackLayout>
<Button class="btn btn-primary btn-active" text="submit" (tap)="submit()"></Button>
</StackLayout>
您的组件应该如下所示:
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { ModalDialogParams } from 'nativescript-angular/modal-dialog';
import { DatePicker } from 'tns-core-modules/ui/date-picker';
import { Page } from 'tns-core-modules/ui/page';
@Component({
moduleId: module.id,
templateUrl: './date-picker-modal.component.html'
})
export class DatePickerModalComponent implements OnInit {
@ViewChild('datePicker') dp: ElementRef;
datePicker: DatePicker;
constructor(private params: ModalDialogParams, private page: Page) { }
ngOnInit() {
this.datePicker = <DatePicker>this.dp.nativeElement;
let currentdate: Date = new Date();
this.datePicker.year = currentdate.getFullYear();
this.datePicker.month = currentdate.getMonth() + 1;
this.datePicker.day = currentdate.getDate();
this.datePicker.minDate = currentdate;
this.datePicker.maxDate = new Date(2045, 4, 12);
}
public submit() {
let selectedDate = this.datePicker.date;
let reserveTime = new Date(
selectedDate.getFullYear(),
selectedDate.getMonth(),
selectedDate.getDate()
));
this.params.closeCallback(reserveTime.toISOString());
}
}
这对我有用:
ngOnInit(): void {
返回未定义。但是从 onLoad 开始使用,@ViewChild
工作正常。
类似
<StackLayout orientation="vertical" id="formie" (loaded)="onLoaded($event)">
在组件上:
export class FormComponent implements OnInit, AfterViewInit {
@ViewChild('datepicker') datepicker: ElementRef;
datePickerReal: DatePicker;
onLoaded(event: EventData) {
this.datePickerReal = <Label>this.datepicker.nativeElement;
我正在开发 NativeScript/Angular 应用程序。我是 NativeScript 的新手,正在学习基于早期版本的教程(我的是 4.1.0,不确定他们使用的是什么)。有一个带有 DatePicker 元素的模式对话框。我想把它 return 选定的日期传给父元素。该应用程序可以正常编译和启动。但是当我尝试打开模式时,出现错误 "Cannot set property 'year' of undefined"。
HTML如下:
<StackLayout class="modal-view-style">
<StackLayout>
<DatePicker id="datePicker"></DatePicker>
</StackLayout>
<Button class="btn btn-primary btn-active" text="submit" (tap)="submit()"></Button>
</StackLayout>
这是 class:
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { ModalDialogParams } from 'nativescript-angular/modal-dialog';
import { DatePicker } from 'tns-core-modules/ui/date-picker';
import { Page } from 'tns-core-modules/ui/page';
@Component({
moduleId: module.id,
templateUrl: './date-picker-modal.component.html'
})
export class DatePickerModalComponent implements OnInit {
constructor(private params: ModalDialogParams, private page: Page) { }
ngOnInit() {
let datePicker: DatePicker = <DatePicker>this.page.getViewById<DatePicker>('datePicker');
let currentdate: Date = new Date();
datePicker.year = currentdate.getFullYear();
datePicker.month = currentdate.getMonth() + 1;
datePicker.day = currentdate.getDate();
datePicker.minDate = currentdate;
datePicker.maxDate = new Date(2045, 4, 12);
}
public submit() {
let datePicker: DatePicker = <DatePicker>this.page.getViewById<DatePicker>('datePicker');
let selectedDate = datePicker.date;
let reserveTime = new Date(
selectedDate.getFullYear(),
selectedDate.getMonth(),
selectedDate.getDate()
));
this.params.closeCallback(reserveTime.toISOString());
}
}
有人知道这是怎么回事吗?我假设只是 api 发生了变化,但我无法在文档中找到它。如果我是对的,将 link 添加到文档中的适当位置将不胜感激。此外,如果这不是执行此类操作的 "preferred" 方法,我很想知道什么是。谢谢。
如果您将 NativeScript 与 Angular 一起使用,则使用 Angular 模板变量语法,如下所示:
<StackLayout class="modal-view-style">
<StackLayout>
<!-- notice the # -->
<DatePicker #datePicker></DatePicker>
</StackLayout>
<Button class="btn btn-primary btn-active" text="submit" (tap)="submit()"></Button>
</StackLayout>
您的组件应该如下所示:
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { ModalDialogParams } from 'nativescript-angular/modal-dialog';
import { DatePicker } from 'tns-core-modules/ui/date-picker';
import { Page } from 'tns-core-modules/ui/page';
@Component({
moduleId: module.id,
templateUrl: './date-picker-modal.component.html'
})
export class DatePickerModalComponent implements OnInit {
@ViewChild('datePicker') dp: ElementRef;
datePicker: DatePicker;
constructor(private params: ModalDialogParams, private page: Page) { }
ngOnInit() {
this.datePicker = <DatePicker>this.dp.nativeElement;
let currentdate: Date = new Date();
this.datePicker.year = currentdate.getFullYear();
this.datePicker.month = currentdate.getMonth() + 1;
this.datePicker.day = currentdate.getDate();
this.datePicker.minDate = currentdate;
this.datePicker.maxDate = new Date(2045, 4, 12);
}
public submit() {
let selectedDate = this.datePicker.date;
let reserveTime = new Date(
selectedDate.getFullYear(),
selectedDate.getMonth(),
selectedDate.getDate()
));
this.params.closeCallback(reserveTime.toISOString());
}
}
这对我有用:
ngOnInit(): void {
返回未定义。但是从 onLoad 开始使用,@ViewChild
工作正常。
类似
<StackLayout orientation="vertical" id="formie" (loaded)="onLoaded($event)">
在组件上:
export class FormComponent implements OnInit, AfterViewInit {
@ViewChild('datepicker') datepicker: ElementRef;
datePickerReal: DatePicker;
onLoaded(event: EventData) {
this.datePickerReal = <Label>this.datepicker.nativeElement;