Angular - Utilizing Reactive Forms for Nested Object Binding

I have created a FormGroup and initialized it with one formControlName called SerialNumber. The JSON structure for SerialNumber is as follows:

{
    "SerialNumber": {
        "snValue": "332432"
    }
}

I am struggling to bind the value of SerialNumber.snValue to the <input>. Instead of displaying 332432, I keep getting [object Object].

.ts code snippet

equipmentForm: FormGroup;
constructor(private fb: FormBuilder) {}
ngOnInit(): void {
     this.equipmentForm = this.fb.group({
     serialNumber: {"snValue": "332432"}
     })
}

.html snippet

<input type="text" formControlName="serialNumber" />

My main question is, how can I properly bind snValue in this scenario?

https://i.stack.imgur.com/j8v3d.png

Answer №1

If you already have an object named obj, you can follow these steps:

obj = {
  "SerialNumber": {
    "snValue": "332432"
  }
};

ngOnInit(): void {
   this.equipmentForm = this.fb.group({
     serialNumber: [obj.SerialNumber.snValue]
   });
}

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

NPM: There are no valid TypeScript file rules specified

Currently working on a small project using React.JS. Whenever I execute : npm run start, the following message gets logged: Starting type checking and linting service... Using 1 worker with 2048MB memory limit Watching: /Users/John/Projects/myProject/src ...

In JavaScript, loop through an array of arrays and combine them using the concat

If I have an array like [["a", "b"], ["c", "d"]], is there a way to iterate, reduce, map, or join this array in order to get the desired output of ["ac", "ad", "bc", "bd"]? What if the array is structured as [["a", "b"], ["c", "d"], ["e", "f"]]; how can we ...

The Subject<T> generic type needs to be provided with 1 type argument

Currently, I am setting up Angular Datatables the Angular Way using Angular 6 and encountering an error that I cannot find in any of the documentation. (TS) Generic type 'Subject' requires 1 type argument(s) When hovering over "Subject" in the ...

Struggling to grasp the concept of Observable Catch closure scope in Angular2?

Seeking guidance on using catch with Observables. I find myself confused and would appreciate some assistance. My goal is to handle a 403 error from the API by deleting the local token and marking the user as unauthenticated in my TokenStore. The approach ...

Module fails to load in the proper sequence

As a .NET developer who is relatively new to modern client-side web applications, I am currently working on developing an Angular2 charting application using Chart.js. The modules are being loaded with SystemJS. Below is the content of my systemjs.config. ...

I am integrating and connecting my angular2 library with my primary project by placing it in the node_modules folder within the library's build directory. This process is expected to

I have developed an Angular2 component as a library and I am connecting this library to my main project. After building my library, a build folder is created. However, when I run npm-link inside the build folder, it generates a node_modules folder with al ...

Troubleshooting a recurring ajax problem with jQuery inside a pop-up dialog box

I am experiencing a peculiar problem and I could really use some help in figuring out where I am going wrong. Within my code, I have a div named "popup" and a button labeled "send_mail". Here's the scenario: when the "send_mail" button is activated, ...

Issue NG0203 encountered during material import attempt

I've been encountering an issue with importing material. Despite using code similar to the examples on material.angular.io, I keep running into the ""inject() must be called from an injection context..." error. My goal is to create a simple table ...

When attempting to transfer data from the parent component to child components, the data is appearing as undefined in the display

I have been working on passing data from a parent component to child components, but I keep encountering an issue where the data is showing as undefined. Below is the code snippet: Parent Component In this section, I have declared the variable part_data ...

switch between choosing, including, and deleting

Presenting a catalog of products, showcasing available products along with all related rate plans. Each rate plan includes a toggle button for adding the product + rate plan to the order. Below are the functions used to add and remove items from the order ...

Modify every audio mixer for Windows

Currently working on developing software for Windows using typescript. Looking to modify the audio being played on Windows by utilizing the mixer for individual applications similar to the built-in Windows audio mixer. Came across a plugin called win-audi ...

Is it possible to extract a single element from an array that is stored as a standard Observable?

Currently, I am using a regular observable instead of an observableArray. This observable keeps an array of elements which is defined as follows: public arrayOfItems: IArrayItem[]; public arrayOfItems$: BehaviorSubject<IArrayItem[]> = new BehaviorSu ...

Using Reactive Forms group in router-outlet

I'm encountering a problem while trying to share my Reactive Forms among multiple components, specifically in the context of routing. The error message I see is: 'Can't bind to 'group' since it isn't a known property of &apos ...

Incorporate OpenLayers 4 into your Angular 5 application

I'd like to integrate OpenLayers 4 into Angular 5 for a project. My main goal is to implement the QuickStart example provided on the official OpenLayers Site. This is what I have accomplished so far: npm install ol --save to download the OpenLayer ...

Is there any need for transpiling .ts files to .js when Node is capable of running .ts files directly?

If you are using node version 12, try running the following command: node hello.ts I'm curious about the purpose of installing typescript globally with npm: npm install -g typescript After that, compiling your TypeScript file to JavaScript with: ...

Confirm the presence of a particular sub collection within Firebase/Firestore by returning true

Can you confirm if the sub-collection named 'categories' exists within the users collection in Firestore? Please return true if it exists and false if it does not. ...

Is it possible to submit a form through a JavaScript hotkey?

Here's the current code that I'm working with: <select tabindex="2" id="resolvedformsel" name="resolved"> <option selected="selected" value="yes">resolved</option> <option value="no">not resolved</option> ...

gulp-angular2 task is malfunctioning

Currently, I am in the process of working on a gulpfile and have written the following task: var tsProject = ts.createProject('app/Resources/public/angular/tsconfig.json'); gulp.task('angular-2', function () { var tsResul ...

AngularJS Constants in TypeScript using CommonJS modules

Let's talk about a scenario where I need to select a filter object to build. The filters are stored in an array: app.constant("filters", () => <IFilterList>[ (value, label) => <IFilterObject>{ value: value, label: label } ]); i ...

Ever tried asynchronous iteration with promises?

I have a specific code snippet that I am working on, which involves registering multiple socketio namespaces. Certain aspects of the functionality rely on database calls using sequelize, hence requiring the use of promises. In this scenario, I intend for t ...