Tips for extracting individual fields from a payload in Angular 8

How do I extract individual fields from the payload and bind them with HTML on a page? I am trying to retrieve the alphacode and countryname in Angular 8.
This is my Component:

this.table$ =  this.productService.get(id)
   .map(actions => {
   
     const id = actions.payload.key;
     const data = actions.payload.val();

    return{id, data};
   
  })
   .subscribe(res =>{

    this.table$ = JSON.stringify(res)

     
  console.log("Value from Firebase "+this.table$)  
   
  return this.table$;
})

I am able to see the output in the console, but I want to display the data separately on the HTML page

{"id":"-MBMklQEALG_eIP2qF1T","data":{"alphacode":"200","countryname":"America","currencyname":"Dollar","numericcode":9111}}

I would like to bind the data to fields [(ngModel)]

<form #f="ngForm" (ngSubmit)="save(f.value)">
    
    <div class="form-group">
        <label for="countryname">Country Name</label>
        <input #countryname="ngModel" [(ngModel)]="table$.countryname" name="countryname" id="countryname" type="text" class="form-control" required >
        <div class="alert alert-danger" *ngIf="countryname.touched && countryname.invalid">
            Country Name is required..!
        </div>
    </div>
    
    <div class="form-group">
        <label for="currencyname">Currency Name</label>
        <input #currencyname="ngModel" [(ngModel)]="table$.currencyname"  name="currencyname" id="currencyname" type="text" class="form-control" required >
        <div class="alert alert-danger" *ngIf="currencyname.touched && currencyname.invalid">
            Currency Name is Required
        </div>
    </div>
    
    <div class="form-group">
        <label for="alphacode">Alpha Code</label>
        <input #alphacode="ngModel" ngModel  name="alphacode" id="alphacode" type="text" class="form-control" required  >
        <div class="alert alert-danger" *ngIf="alphacode.touched && alphacode.invalid">
            Alpha Code is required..!
        </div>
    </div>

    <div class="form-group">
        <label for="numericcode">Numeric Code</label>
        <div class="input-group">
        <span class="input-group-text">$</span>
        <input #numericcode="ngModel"  ngModel name="numericcode" id="numericcode" type="number" class="form-control" required>
    </div>
    <div class="alert alert-danger" *ngIf="numericcode.touched && numericcode.invalid">Price Required</div>
    </div>

    <div class="form-group">

    <mat-radio-group aria-label="Select an option">
        <mat-radio-button value="1">Active</mat-radio-button>
        <mat-radio-button value="2">In-Active</mat-radio-button>
      </mat-radio-group>
    </div>
      

    <button class="btn btn-primary">Save</button>

</form>

Answer №1

const info = {
  "category": "details",
  "dataInfo": {
    "alphacode": "200",
    "countryname": "America",
    "currencyname": "Dollar",
    "numericcode": 9111
  }, "pin": "-MBMklQEALG_eIP2qF1T"
}

const alphacodeValue = info.dataInfo.alphacode;
const country = info.dataInfo.countryname;

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

Filter multiple columns in an Angular custom table with a unique filterPredicate

Looking to develop a versatile table that accepts tableColumns and dataSource as @Input(). I want the ability to add custom filtering for each table column. Currently, I've set up the initialization of the table FormGroup and retrieving its value for ...

Managing the display of numerous ngFor components

If you're interested in learning more about the features I will include, here's a brief overview. I plan to have a project section with cards displayed for each project, all populated from a JSON file. When users click on a card on the website, a ...

How can I invoke the header component function in another component using Angular 2?

Is there a way to invoke the showmodel(displayType) function from another component? How can I call a function in the header component from a different component? header.component.ts import { Component,Renderer } from '@angular/core'; i ...

What could be the reason for Angular 2 not recognizing this valid regex pattern?

The regular expression shown below is considered valid (check here): ^(\+27|27|0)\s?(\d{2})[-\s]?(\d{3})[-\s]?(\d{4})$ Despite its validity, Angular 2 throws the following error: EXCEPTION: Error in ./App class App - i ...

sending arguments to angular directive

It was causing me some concern to see the number of global Angular change detections triggered every time an event occurs. That's why I stumbled upon Angular 2 runOutsideAngular still change the UI and decided to implement the 'OutSideEventHandle ...

Error encountered when creating a new project using ASP.NET Core (.NET 5) and Angular 11

When I start a new ASP.NET Core Web API + Angular project in Visual Studio by using: dotnet new angular, it sets up a .NET 5 project with an Angular 8.2 project in the ClientApp folder. After hitting F5, everything runs smoothly. However, I want to work ...

What is the process for eliminating moment locales from an Angular build?

When I build my Angular 5 application with the command: ng build --prod --sm I noticed that the main.js file contains a lot of excess space occupied by moment. It seems all the locales are being loaded when I include: import * as moment from 'momen ...

The content of the string within the .ts file sourced from an external JSON document

I'm feeling a bit disoriented about this topic. Is it feasible to insert a string from an external JSON file into the .ts file? I aim to display the URLs of each item in an IONIC InAppBrowser. For this reason, I intend to generate a variable with a sp ...

Access the $event object from an Angular template selector

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script> <input type="file" #myFile multiple /> <button (click)="onDelete(myFile.event)">DeleteFiles</button> My code snippet is experienci ...

Modifying the date format of the ag-Grid date filter

On my Angular 5.2.11 application, I utilize ag-grid to showcase a table. The date column is configured with the default date filter agDateColumnFilter as per the documentation. After enabling browserDatePicker: true, the Datepicker displays dates in the ...

What is the best way to incorporate multiple pages and export them in an angular2 project?

Having some issues with my code. Can anyone lend a hand? import { Component } from '@angular/core'; @Component({ selector: 'my-app', templateUrl: `page1.html` }) @Component({ selector: 'my-app2', templateUrl: `page2.html` ...

Is there an issue with integrating Bootstrap with Angular 6?

npm install bootstrap Update the angular.json file: "styles": [ "node_modules/bootstrap/dist/css/bootstrap.min.css", "styles.scss" ] Include the Bootstrap CSS directly in your src/style.css: @import '~bootstrap/dist/css/bootstrap.min.css'; A ...

Utilizing Angular to automatically extract keys from nested objects in a response

In my Angular application, I am facing a challenge with accessing nested responses from the server. The data structure contains multiple responses within one parent object, and I am struggling to dig deeper into it. Here is the code snippet I have so far: ...

6 Ionic date-time selector

I seem to be encountering some challenges while using Ionic 6 with the new date-time picker. The issue arises when I retrieve a value from the database through a nest service. In the database, the date appears as: “2022-06-30 13:11:54” but upon retriev ...

How can Angular CLI prevent the submission of an HTML form unless a previous option has been selected

I am currently working on a form that contains select fields with various options pre-populated. <form [formGroup]="selectVehicleForm"> <select formControlName="Manufacturer"> <option *ngFor='let ...

When utilizing a Service through UserManager, the User variable may become null

Utilizing Angular 7 along with the OIDC-Client library, I have constructed an AuthService that provides access to several UserManager methods. Interestingly, when I trigger the signInRedirectCallback function from the AuthService, the user object appears ...

Guidelines for accessing a specific object or index from a dropdown list filled with objects stored in an array

Here is a question for beginners. Please be kind. I have created a select field in an HTML component using Angular, populated from an array of objects. My goal is to retrieve the selection using a method. However, I am facing an issue where I cannot use ...

What is the method to modify the starting point of the animation for the material component "mat-progress-bar" so it initiates from 0 instead of 100

I recently integrated a material progress bar into my Angular project. Here is the code snippet: <mat-progress-bar mode="determinate" value="40"></mat-progress-bar> However, I encountered an issue where upon page refresh, ...

Error: Unable to retrieve data - Angular2 application encountered a XHR error with status code 404 (

Hey there, I recently started working with angularjs and created a demo app using angular2. However, when I try to run the application, I encounter an error. Interestingly, even though the error mentions a 404 status code, if I visit the URL http://loc ...

JavaScript ECMAScript 6 - WARNING: "Decorators can only be applied to a class when exporting"

In ECMAScript 6, I am attempting to export a function so that I can import it and utilize it in other files for the sake of writing DRY code. However, an error message is appearing: You can only use decorators on an export when exporting a class (16:0) ...