Automatically save form controls with formControlName in Nativescript

I have placed a TextField in my .html file

<TextField class="textfield" row="0" col="1" formControlName="powersupplyid" [text]='myipdevice'> </TextField>

I am looking for a way to automatically save the value when 'myipdevice' is displayed on the screen.

getform() {
    ..........
     .....
        this.myipdevice = myipfind;
        console.log('Value found', this.myipdevice)
        let LS = require("nativescript-localstorage");
        LS.setItem(this.myipdevice);
    }

Does anyone have an idea on how to automatically save the value of myipdevice when it is shown in the html?

Thank you!

Answer №1

The information is already stored in your form object under the name you assigned to it. For example, if you named it "form"

After that,

this.form.controls.powersupplyid.value
will be automatically updated with any input or action taken within the textarea.

Answer №2

Currently, you seem to be handling two tasks simultaneously. One one hand, you are associating the TextField with a reactive form field using formControlName, while on the other hand, you are also linking the [text] property. It is recommended to stick to using only formControlName.

In your component, after retrieving the value, you can update the control's value accordingly. (Assuming certain details since the full code was not provided.)

For instance, in the ngOnInit method in the example, you can fetch the stored value and then use patchValue to reflect this change in the view.

Moreover, you can monitor changes to the control by subscribing to valueChanges.

export class IpDeviceFormComponent implements OnInit {

  form: this.fb.group({
    powersupplyid: [''] // FormControl bound to TextField
  });

  constructor(private fb: FormBuilder) { }

  ngOnInit() {
    // retrieve a previously saved value
    getSavedPowerSupplyId().subscribe(id => {
      this.form.patchValue({
        powersupplyid: id
      });
    });

    // listen for form changes
    this.form.valueChanges.subscribe(form => { /* Save new form state */ });
  }
}

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

Utilizing ReadableStream as a body for mocking the HTTP backend in unit testing for Angular 2

Looking to simulate the http backend following this helpful guide. This is my progress so far: Test scenario below describe('DataService', () => { beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ ...

A guide on effectively implementing CSS preprocessors in Angular 7

Exploring the world of pre-processors is new to me. I’m curious about how to integrate them, specifically postcss and lostgrid, with angular 7. I'm attempting to incorporate this code snippet into my angular project. The code relies on postcss-cssn ...

Angular 4 emerges as the prime choice for efficiently sharing large object data services

I'm in the process of determining the most effective approach (Observables, BehaviorSubject, Redux, etc.) to create a service for sharing data among unrelated components. Within my Data Service, there is a significant array of items (exceeding 10k) t ...

unable to modify the attributes of an object in Angular

I've been tasked with modifying an existing Angular project that includes a component where I have the following variable: public testRunDetails: ITestRunDetails[] = []; The variable testRunDetails is of type ITestRunDetails, which is defined as: exp ...

Utilizing Typescript to Inject Generics and Retrieve the Name of an ES6 Module

I am currently working on developing a versatile repository using: Typescript ES6 Angular 1.x However, I am facing challenges in determining the correct way to inject the Entity and retrieve its module name. The main reason for needing the name: I adh ...

Mastering Angular2: Leveraging TypeScript's Power to Unleash JavaScript ES6 Syntax with

I am having trouble implementing a translation feature using the ng2-translate pipe in my Angular2/Ionic2 app, which is entirely written in JavaScript ES6. However, I have encountered an issue during the setup phase. The code snippets I have found on the ...

How can I bind form data to an array in Angular?

import { Component, OnInit } from '@angular/core'; import { Senderv3 } from 'app/models/codec/senderv3'; import { CustomerInfoService } from '../../../services/customer-info.service'; import { Router } from '@angular/rout ...

Generating a dynamic clickable div using Angular 6

How can I create a user-friendly interface that displays an image with clickable divs around detected faces, allowing users to view specific attributes for each face? I'm looking for a solution where I can have divs or buttons around each face that t ...

Creating formGroups dynamically for each object in an array and then updating the values with the object data

What I am aiming to accomplish: My goal is to dynamically generate a new formGroup for each recipe received from the backend (stored in this.selectedRecipe.ingredients) and then update the value of each formControl within the newly created formGroup with t ...

Creating a React component with a column allowing multiple checkbox selections in NextUI table

Setting up multiple "checkbox" columns in a table using the NextUI table has been my current challenge. Each row should have selectable checkboxes, and I want these selections to be remembered when navigating between pages, running searches, or removing co ...

What is the best way to simulate DOCUMENT dependency injection for unit tests using Jest?

Currently, I have integrated a component known as 'A' which relies on the document object for its functionality. The component features a method called onClose(), which utilizes the document object service to retrieve elements using querySelector ...

Subscription to Observable content failed to run

When a submit button is clicked inside the component HTML, it triggers a function called addCollaborators(). The code for this function can be found below: component.ts emails: string[] = []; constructor(public userService: UserService) {} // Function ...

Convert image file to a React TypeScript module for export

Imagine a scenario where you have a folder containing an image file and an index file serving as a public API. Is there a method to rename the image file before reexporting it? Here is the structure of the folder: └── assets/ ├── index.t ...

Maximizing the potential of Angular forms through native FormData

Currently, I am revisiting an older project that still uses traditional methods for form submission. The HTML includes a form element with defined method and action. My goal is to submit the form in the old-fashioned way using the action attribute to post ...

Should I opt for 'typeof BN' or the BN Constructor when working with TypeScript and "bn.js"?

Note: Despite the recommendation to use BigInts instead of bn.js, I am currently working with a legacy codebase that has not been migrated yet. Below is the code that compiles and executes without any issues: import { BN } from "bn.js"; import a ...

angular change digits to Persian script

I am trying to convert English numbers to Persian numbers in angular 4: persianNumbers = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"]; engli ...

MXGraph has an issue where edges fail to be redrawn after being moved

Perhaps this question may seem trivial, but I am facing an issue in my code and seeking guidance from the community. I am working on a javascript mxGraph application within Angular7. Specifically, I have modified the ports.html example for my project. Wh ...

Error encountered while working with SVG in a functional React component with typescript

I have a customized icon component that looks like this: import React from 'react'; type IconProps = { width?: number; height?: number; color?: string; styleName?:string; }; const MyIcon: React.FC<IconProps> = ({ width ...

Configuring web.config for Angular Universal deployment

I am trying to deploy Angular 4 Universal on Azure Web, but I am encountering some issues with the web.config file. The server.js file is located in the dist folder, so I set the path in the web.config as "dist/server.js". However, when the server.js runs ...

ts:Accessing the state within a Redux store

In my rootReducer, I have a collection of normal reducers and slice reducers. To access the state inside these reducers, I am using the useSelector hook. Here is my store setup : const store = configureStore({reducer : rootReducer}); Main Reducer: const ...