Enhance Angular Forms: Style Readonly Fields in Grey and Validate Data Retrieval using Typescript

Is there a way to disable a form and make it greyed out, while still being able to access the values?

https://i.sstatic.net/wt2c3.png

1/ Disabling controls with

this.roleForm.controls['id'].disable()
in Typescript does not allow me to retrieve the values of Id, Name, and Description afterwards.

this.roleForm = new FormGroup({
  id: new FormControl(this.singleRole?.id),
  name: new FormControl(this.singleRole?.name),
  createDate: new FormControl(this.singleRole?.createDate),
  status: new FormControl(this.singleRole?.status)
});

this.roleForm.controls['id'].disable();
this.roleForm.controls['name'].disable();
this.roleForm.controls['createDate'].disable();

I won't be able to see the values for the three mentioned above.

console.log(this.roleForm.value)

2/ Utilizing a Fieldset will achieve the desired effect of greying out values but still allowing them to be accessed. *I am aiming to implement this functionality using Typescript rather than HTML.

<fieldset class="data-form" [disabled]="true">
    <mat-form-field class="row" appearance="outline" floatLabel="always">
        <mat-label>Id</mat-label>
        <input matInput formControlName="id" [value] = "singleRole.id" >
    </mat-form-field>

    <mat-form-field class="row" appearance="outline" floatLabel="always">
        <mat-label>Name</mat-label>
        <input matInput formControlName="name" [value] = "singleRole.name" >
    </mat-form-field>

    <mat-form-field class="row" appearance="outline" floatLabel="always">
        <mat-label>Created Date</mat-label>
        <input matInput formControlName="createDate" [value] = "singleRole.createDate | date: 'MM/dd/yyyy'" >
    </mat-form-field>
  </fieldset>

Answer №1

If you encounter difficulty retrieving the values of disabled form controls using the formgroup.value method, consider utilizing formGroup.rawValue instead. This alternative approach is specifically designed to capture the values of disabled elements as well.

Answer №2

One approach to consider is utilizing [formControl]="id" as it simplifies the process of retrieving values from an Input tag.

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

Issue with Angular 8 docker container: module not found despite being installed

I am currently working on an Angular 8 single-page application (SPA). Utilizing a Dockerfile that is managed and executed through a Docker Compose setup. Everything was functioning smoothly for weeks until today when, out of nowhere, the setup stopped wor ...

Filtering nested arrays in Javascript involves iterating through each nested

I have a nested array inside an array of objects in my Angular app that I'm attempting to filter. Here is a snippet of the component code: var teams = [ { name: 'Team1', members: [{ name: 'm1' }, { name: 'm2' }, { name ...

PNG file is not displayed on React TSX page despite absence of any errors

I've implemented this initial design template from GitHub (https://github.com/vikpe/react-webpack-typescript-starter). Additionally, I'm utilizing react-bootstrap and have a container that includes a backgroundImage. const heroImage = require(&q ...

The Chrome debugger fails to display variable values when hovering the mouse over them

After creating a basic React app using the command "npx create-react-app my-app --template typescript", I encountered an issue where the values were not appearing in Chrome dev tools when I added a breakpoint in the code. Is this expected behavior for a Re ...

Having issues with @ts-ignore in Typescript on a let variable that is not reassigned?

JOURNEY TO THE PROBLEM My current task involves destructuring a response obtained from an Apollo useLazyQuery, with the intention to modify one variable. In a non-Typescript environment, achieving this would be straightforward with just two lines of code: ...

What enables typescript to be eligible for transpiling is its equivalent level of abstraction to javascript?

Transpilation is the act of converting code from one language to another with a similar level of abstraction. Can you point out some distinctive characteristics that indicate TypeScript transpires into JavaScript? ...

Is there a method to transform the event triggered by a child component via @Output into an observable stream within the parent component?

If we want to trigger a click event from a child component to the parent component, we can use @output in the child component and listen for that event in the parent component using the following syntax: <app-item-output (newItemEvent)="addItem($e ...

Customizing the background-color of a div to match the primary theme color using Angular 2 Material

I'm currently implementing a theme from the Angular 2 Material library. I have successfully applied the theme colors to material components such as buttons and toolbars using color="primary". However, I am facing difficulties in theming a div element. ...

Obtain access to the interface from the base class

I am looking for a way to define an interface in a child class that can be accessed by a method in the parent abstract class. For instance, consider the following code snippet: export default abstract class MyClass { protected foo(arg: this.myInterfac ...

Unresolved promise: Internal server issue

I encountered an exception while working on my Nativescript app. EXCEPTION: Uncaught (in promise): Server error JS: ORIGINAL STACKTRACE: JS: Error: Uncaught (in promise): Server error JS: at resolvePromise (/data/data/com.yourdomain.appname/files/app/ ...

Press the second form submit button after the completion of another Observable

Below is the unique process we aim to accomplish using solely RXJS Observables: Press Login button (form with username & password). We bind Observable.fromEvent with our form. Upon submitting the form, call loginUser() to send an http request to serv ...

Requesting data through AngularJS2 HTTP call results in the return of complete PHP code

I am currently facing an issue where my http AJAX call to a local PHP file is echoing the complete PHP code. Since I am new to AngularJS2, I would appreciate some help. Here is the snippet of my AngularJS2 code: makeHttpCall():Promise<any>{ ret ...

The ViewChild element is not defined

Check out the code snippet below. // ... @ViewChild('searchBar', {static: false}) searchBar: IonSearchbar; @ViewChild('locations', {static: false}) locationsList: IonList; // ... ngAfterViewInit() { this.searchBarInputSub ...

Facing issues with the template URL not functioning properly during the migration from Angular 1.5 to Angular 6

I am currently in the process of migrating an Angular 1.5 project to Angular 6, but I've encountered an issue with the templateUrl not working in the Angular 1.5 component. The packages I am using are: Angular CLI 6.0.8 TypeScript 2.7.2 Angular 6.0.7 ...

Working with Ngxs and WebSocket: How to intercept the connection and include a custom HTTP header

Currently, I am utilizing ngxs's NgxsWebsocketPluginModule to establish a websocket connection within my Angular application. In order to authenticate the connecting client, my server mandates an authentication token to be included in the HTTP headers ...

Is there a way to determine the quantity of lines within a div using a Vue3 watcher?

Is it feasible to determine the number of text lines in a div without line breaks? I am looking to dynamically display or hide my CTA link based on whether the text is less than or equal to the -webkit-line-clamp value: SCRIPT: const isExpanded = ref(true ...

Emphasize the chosen row in Primeng table without needing to select the checkbox

Currently, I am utilizing the primeng library to implement a p-table combined with a p-checkbox. My goal is to enable the highlighting of a clicked row (or its content) without automatically checking the checkbox. The intention is for the Checkbox to only ...

Verify the dates in various formats

I need to create a function that compares two different models. One model is from the initial state of a form, retrieved from a backend service as a date object. The other model is after conversion in the front end. function findDateDifferences(obj1, ...

Google Maps API Version 3 now allows for custom overlays to be hidden when they overlap

I have implemented multiple custom overlays on a map for various cities and I am trying to manage the overlapping ones by hiding or collapsing them. My goal is to display and expand overlays with the highest population whenever there is available space. M ...

When using an Angular2 application that relies on an external reference to Auth0Lock, the application encounters a NotFound error when executed

For my latest project, I decided to create an app using angular2-seed as a base. Everything was going smoothly until I tried deploying the production build on an Azure website. To my surprise, I encountered an issue when trying to load the page: > Refe ...