"Experiencing issues retrieving user-modified values in NativeScript's TimePicker component

I'm having trouble retrieving the user-modified value from a TimePicker. Instead of getting the modified value, I keep receiving the original value that was initially set in the control.

Below is my component template:

<TabView [(ngModel)]="tabSelectedIndex" selectedColor="#FF0000" style="height:90%">
    <StackLayout *tabItem="{title: 'Time'}">
        <TimePicker #timePicker [(ngModel)]='model.time'></TimePicker>
    </StackLayout>
    <StackLayout *tabItem="{title: 'Date'}">
        <DatePicker #datePicker [(ngModel)]='model.date'></DatePicker>
    </StackLayout>
</TabView>
<button text="Done" (tap)="onDoneTap()"></button>

And here is my component code:

export class DateTimePickerComponent 
{
    public mDate: Date;
    public tabSelectedIndex: number;
    public model: any;

    constructor() 
    {
        this.mDate = new Date();
        this.model = {
            date: new Date(2014, 5, 13, 2, 24),
            time: new Date(2014, 5, 13, 2, 24)
        };
    }

    private onDoneTap()
    {
        this.mDate.setMinutes(this.model.time.getMinutes());
        this.mDate.setHours(this.model.time.getHours());

        this.mDate.setDate(this.model.date.getDate());
        this.mDate.setMonth(this.model.date.getMonth());
        this.mDate.setFullYear(this.model.date.getFullYear());  

        console.log(this.mDate);   
    }
}

To create this, I referred to the official documentation:

  • For DatePicker - utilize the date property
  • For TimePicker - utilize the time property

The DatePicker in this scenario functions as expected; it initializes at 2014-05-13 and accurately reflects any changes made when tested. However, the issue arises with the TimePicker. Am I making an error specifically concerning the TimePicker implementation?

Note: I attempted to access the hour and minute properties using the ID directly but was unsuccessful. The initial value of 2:24 always persists.

@ViewChild("timePicker") timePicker: ElementRef;
let datePickerView = <TimePicker>this.timePicker.nativeElement;
console.log(datePickerView.hour);  //  2
console.log(datePickerView.minute) // 24

Answer №1

One common issue might arise if you forget to include NativeScriptFormsModule in the imports section of your main.ts file. Check out the example below for clarification:

import { platformNativeScriptDynamic, NativeScriptModule } from "nativescript-angular/platform";
import { NativeScriptFormsModule } from "nativescript-angular/forms";
import { NgModule } from "@angular/core";
import { AppComponent } from "./app.component";

@NgModule({
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    imports: [NativeScriptModule, NativeScriptFormsModule],
})
class AppComponentModule {}

platformNativeScriptDynamic().bootstrapModule(AppComponentModule);

If you encounter any issues, feel free to utilize the TimePicker value as an argument in your onDoneTap method. Refer to the sample code snippet provided below.

app.component.html

<StackLayout  exampleTitle toggleNavButton>
<timePicker class="example-container" #timePicker  verticalAlignment="center"></timePicker>
<Button text="view" (tap)="ontap(timePicker.time)"></Button>

</StackLayout>

app.component.ts

export class AppComponent {
    public ontap(args:Date){
        console.log(args);
    }
}

Answer №2

In case someone stumbles upon this:

There was actually a bug reported on Github regarding this issue. You can check it out here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Setting up Material-UI for React in conjunction with Typescript: A step-by-step guide

I've encountered issues while trying to integrate Material UI into my React project that's written in Typescript. Following the tutorial, I began by adding the react-tab-event-plugin. import injectTapEventPlugin from 'react-tap-event-plugi ...

Illustrative demonstration of AngularJS

One way to showcase data using AngularJS is by triggering a function with a button click. Here's an example: <!DOCTYPE html> <html ng-app> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"& ...

Unable to restore previous state when the page is refreshed

I've built a single page application using angular v1.5.6 and angular-ui-router v1.0.0alpha0. You can view the Working Plunker here. In this SPA, I have three main pages: the landing page which is "/", /Home, and /About. The Home page requires login; ...

The expected input should be either an HTMLElement or an SVGElement, but the received input is currently null

Below is the code for a component: function SignUpPage() { return ( <> <h1>Sign Up</h1> <input name="userName" /> </> ); } export default SignUpPage; Testing the component: it("should c ...

The functionality of window.localStorage.removeItem seems to be malfunctioning when used within an $http

Currently, I am sending a post request to my web service and upon successful completion, I am attempting to remove a token from local storage. Here is the code snippet: $http.post("MyService/MyAction").success(function (res) { if ( ...

Learn how to easily toggle table column text visibility with a simple click

I have a working Angular 9 application where I've implemented a custom table to showcase the data. Upon clicking on a column, it triggers a custom modal dialog. The unique feature of my setup is that multiple dialog modals can be opened simultaneously ...

Encountering errors in Visual Studio when trying to work with node_modules directories that have a tsconfig

In my current project, there is a tsconfig.json file located in the root directory. Strangely, Visual Studio keeps throwing errors related to other instances of tsconfig.json found in different packages, as shown below: https://i.sstatic.net/T7Co2.png Ev ...

How can I utilize npm with the original source code instead of minified or bundled code?

I am looking to access npm and JavaScript (or TypeScript) 3rd party libraries directly from the source code. Similar to how I can make changes in Python libraries by going into their source code, I want to have the same capability with my JavaScript depen ...

Adjusting image dynamically based on conditions

I need to dynamically display images on my HTML based on specific conditions using TypeScript. In my TypeScript file: styleArray = ["Solitary", "Visual","Auditory","Logical","Physical","Social","Verbal",]; constructor(){ for (var i = 0; this.sty ...

What sets apart the usage of "protractor protractor.conf.js" from "npm run e2e" command?

Can you explain the difference between these two commands when running them in an Angular CLI project terminal? 'protractor protractor.conf.js' --> Launches the baseurl specified in protractor.conf.js instead of localhost 'npm run e2e&a ...

JavaScript Looping through multiple files for upload will return the last file in the series

I'm currently working on implementing a multiple file upload feature using JavaScript. Within my HTML, I have the following input: <input type="file" (change)="fileChange($event,showFileNames)" multiple /> When the onChange event is triggere ...

Creating a Gulp automation task to handle ng-constant across various environments

I've been attempting to make this work, but it seems like I might be overlooking something. I'm utilizing ng-constant and configuring different environment endpoints as outlined in the ng-constants issue However, my setup involves using gulp and ...

I'm curious as to why my array is only being filled within the subscription function

When I call the GetAllMainStore() function, data is successfully retrieved from the API and the console indicates that "this.MainStoreArray" contains data. The problem arises when I attempt to access "this.MainStoreArray" outside of the GetAllMainStore() ...

What specific files from the Kendo core are required for utilizing Mobile and Angular functionalities?

After browsing through similar questions, I couldn't find a solution. Currently, I am experimenting with Kendo (open source core for now) in a Visual Studio Cordova project. Initially, disregarding Cordova, I am focusing on setting up a basic view wit ...

What is the best way to reset a nested object in AngularJS?

Currently, I am learning from an angular.js tutorial. In this tutorial, I have successfully added a list feature to my object array and created a form to add new items to the list. However, I faced some challenges when trying to figure out how to reset the ...

In Angular 2, templates may not be fully executed when utilizing ngAfterContentInit or ngAfterViewInit functions

When I try to access the DOM of the document, I notice that the template is only partially executed. Using a setTimeout helps me bypass the issue temporarily, but what is the correct way to handle this? import { Component, Input, AfterContentInit } from & ...

A Promise-based value returned by a Typescript decorator with universal methods

I am currently working on creating a method decorator that can be applied to both prototype and instance methods. Referenced from: Typescript decorators not working with arrow functions In the code provided below, the instanceMethod() is returning a Prom ...

Enhancing the appearance of the Mui v5 AppBar with personalized styles

I am encountering an issue when trying to apply custom styles to the Mui v5 AppBar component in Typescript. import { alpha } from '@mui/material/styles'; export function bgBlur(props: { color: any; blur?: any; opacity?: any; imgUrl?: any; }) { ...

How can I arrange a table in Angular by the value of a specific cell?

Here's the current layout of my table: Status Draft Pending Complete I'm looking for a way to sort these rows based on their values. The code snippet I've been using only allows sorting by clicking on the status header: onCh ...

Transmitting the Ctrl+A key combination to an element

My current testing framework is protractor, which I utilize for conducting end-to-end tests in Angular. When it comes to inputting text into an element, I typically use: element(by.model('myModel')).sendKeys('Test'); However, I am cu ...