I updated my NS Angular app from 2.5 to 3.0.0, and found that my code which bind to the "propertyChange" event on DatePicker, TimePicker and ListPicker fields no longer works. I tried the Nativescript Angular sample project and also can re-produce the problem.
Both
tns --version to fetch it)3.0.0
node_modules/tns-core-modules/package.json file in your project)โ Component โ Current version โ Latest version โ Information โ
โ nativescript โ 3.0.0 โ 3.0.0 โ Up to date โ
โ tns-core-modules โ 3.0.0 โ 3.0.0 โ Up to date โ
โ tns-android โ 3.0.0 โ 3.0.0 โ Up to date โ
โ tns-ios โ 3.0.0 โ 3.0.0 โ Up to date โ
<StackLayout sdkExampleTitle sdkToggleNavButton>
<!-- >> creating-datepicker-html -->
<DatePicker id="myDatePicker" #datePicker (loaded)="configure(datePicker)" verticalAlignment="center"></DatePicker>
<!-- << creating-datepicker-html -->
</StackLayout>
import { Component, OnInit } from "@angular/core";
import { DatePicker } from "ui/date-picker";
import { Page } from "tns-core-modules/ui/page";
@Component({
moduleId: module.id,
templateUrl: "./configure-date-picker.component.html"
})
export class ConfigureDatePickerComponent implements OnInit {
myDatePicker: DatePicker;
constructor(private page: Page) {};
configure(datePicker: DatePicker) {
datePicker.year = 1980;
datePicker.month = 2;
datePicker.day = 9;
datePicker.minDate = new Date(1975, 0, 29);
datePicker.maxDate = new Date(2045, 4, 12);
}
ngOnInit(): void {
this.myDatePicker = <DatePicker>this.page.getViewById('myDatePicker');
this.myDatePicker.on("loaded", (propertyChangeData) => {
console.log(`----------date picker loaded`);
});
this.myDatePicker.on("propertyChange", (propertyChangeData) => {
console.log(`----------property changed`);
});
}
}
Outcome:
Hi @clarenceh,
Thank you for reporting this issue.
With the new NativeScript release 3.0 we introduce some breaking changes with our propertyChange system.
At this time you should specify the name of the property in on method, which changes you would like to handle. For example, for the DatePicke, you should use dateChange event name.
TypeScript
this.myDatePicker.on("dateChange", (args) => {
console.log(`----------property changed`);
});
Hi @tsonevn ,
I am not able to achieve two-way data binding for DatePicker by using the Nativescript SDK examples (https://github.com/NativeScript/nativescript-sdk-examples-ng/tree/master/app/ui-category/date-picker/configure-date-picker), can you please let me know how can I achieve two-way data binding in DatePicker ?
Hi @srnaik,
I tested two-way binding on my side for DatePicker component and it seems to work as expected. You could review the attached sample code below.
HTML
<StackLayout sdkExampleTitle sdkToggleNavButton>
<!-- >> creating-datepicker-html -->
<DatePicker #datePicker verticalAlignment="center" [(ngModel)]="date"></DatePicker>
<Button text="Show new Date" (tap)="onTap($event)"></Button>
<!-- << creating-datepicker-html -->
</StackLayout>
TypeScript
import { Component } from "@angular/core";
import { DatePicker } from "ui/date-picker";
@Component({
moduleId: module.id,
templateUrl: "./configure-date-picker.component.html"
})
export class ConfigureDatePickerComponent {
public date:Date;
configure(datePicker: DatePicker) {
}
ngOnInit(){
this.date = new Date(2040, 4, 13);
}
onTap(args){
console.log("print new date");
console.dir(this.date);
}
}
Note that in the example above I had to change [(date)] to [(ngModel)] to make the date property correctly update.
Thanks for the reply @tsonevn and @EddyVerbruggen , I am able to get two-way data binding for both Date-Picker and Time-Picker. And now I am trying to implement two-way data binding for Date-Time field, for now I am able to bind the Date part of the Date-Time field to Date-Picker and Time part of the Date-Time field to Time picker, However I am not able to change/modify these Date &Time values using the Date and Time picker.
How can I implement two-way data binding for the Date-Time field using Date-picker and Time-Picker ?
Do we have a separate Date-Time Picker in Nativescript?
Hi @srnaik,
If I understand you correctly, you are trying to handle the dateChange event and to display the newly selected value in the TextField.
If this is the case you could review the below-attached sample code, where is shown how to handle the dateChange event and how to change the value of the TextField text property.
HTML
<StackLayout sdkExampleTitle sdkToggleNavButton>
<!-- >> creating-datepicker-html -->
<TextField #tf hint="date" ></TextField>
<DatePicker #datePicker verticalAlignment="center" (dateChange)="tf.text = datePicker.date" [(ngModel)]="date"></DatePicker>
<Button text="Show new Date" (tap)="onTap($event)"></Button>
<!-- << creating-datepicker-html -->
</StackLayout>
TypeScript
import { Component, OnInit } from "@angular/core";
import { DatePicker } from "ui/date-picker";
import { Item } from "./item";
import { ItemService } from "./item.service";
@Component({
selector: "ns-items",
moduleId: module.id,
templateUrl: "./items.component.html",
})
export class ItemsComponent implements OnInit {
public date:Date;
configure(datePicker: DatePicker) {
}
ngOnInit(){
this.date = new Date(2040, 4, 13);
}
onTap(args){
console.log("print new date");
console.dir(this.date);
}
}
Thanks @tsonevn , I am able to achieve this functionality !
Where can I see a list of available working event names for Nativescript 3.0?
I'm trying to get the height of a StackLayout when the keyboard shows up using the "propertyChange" event but it never runs.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
Where can I see a list of available working event names for Nativescript 3.0?
I'm trying to get the height of a StackLayout when the keyboard shows up using the "propertyChange" event but it never runs.