我正在使用NativeScript / Angular应用程序。我是NativeScript的新手,并且正在学习基于早期版本的教程(我的版本是4.1.0,不确定他们使用的是什么)。有一个带DatePicker元素的模式对话框。我希望将所选日期返回到父元素。该应用程序可以编译并正常运行。但是,当我尝试打开模式时,出现错误“无法设置未定义的属性'year'”。
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>
这是课程:
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已更改,但是我无法在文档中找到它。如果我对此表示正确,那么将非常感谢您提供指向文档中相应位置的链接。另外,如果这不是做这种事情的“首选”方式,我很想知道是什么。谢谢。
答案 0 :(得分:2)
如果将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());
}
}
答案 1 :(得分:0)
这对我有用:
NativeScript angular nativeView undefined
ngOnInit(): void {
返回未定义。但是@ViewChild
可以从onLoad上正常使用。
类似
<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;