Ensuring that the field remains active in Angular2 even after editing it with a value

When the 3rd dropdown value is selected in the first field dropdown, it enables the 2nd field. However, when I edit and change a different value, since the 2nd field is disabled, I receive null or undefined as the value. I want the 2nd field to retain its value if the 1st field is not changed in edit mode.

HTML:

TS:

private FormInit() {
    //Add
    if (!this.Details) {
      this.detailsForm = this.FB.group({
        Type: ['', Validators.required],
        ID: [{ value: 0, disabled: true }],
      });

    } else {
      // edit
      this.detailsForm = this.FB.group({
        Type: [this.Details.data.Type, Validators.required],
        SourceID: [{ value: this.Details.data.ID, disabled: true }],

      });
      if (this.Details.mode == 'readOnly') {
        this.detailsForm.disable();
      }
    }
  }

ADD and Edit functions:

public save() {
    let addParams = this.detailsForm.value;
   addParams.Type = addParams.Type ? addParams.Type: 0;
    this.Service.add(addParams).subscribe(res => {
      console.log(res);
      this.successMessagePopUp(res);
    })
  }

  public update() {
    let updateParams = this.detailsForm.value;

    this.Service.update(updateParams).subscribe(res => {
      this.successMessagePopUp(res)
    })
  }

I am looking for a solution where, when the 3rd dropdown value is selected, the 2nd field remains enabled in edit mode and only gets disabled if another value is selected?

Answer №1

Typically, when disabled values are used in angular, they appear as undefined.

Note: Based on your requirements, here is a suggestion:

private FormInit() {
    if (!this.Details) {
      this.detailsForm = this.FB.group({
        Type: ['', Validators.required],
        ID: [{ value: 0, disabled: true }],
      });

    } else {
      // edit
      this.detailsForm = this.FB.group({
        Type: [this.Details.data.Type, Validators.required],
        ID: [{ value: this.Details.data.ID }],

      });

     if (this.Details.data.Type == 3)
     {
         this.detailsForm.get("ID").enable();
     }
     else
     {
         this.detailsForm.get("ID").disable();
     }

      if (this.Details.mode == 'readOnly') {
        this.detailsForm.disable();
      }
    }
  }

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

ngx-translate is failing to display any text within a lazily-loaded module

We are currently utilizing Angular 6 within our application and have recently initiated the process of preparing our app for lazy-loading. Our application consists of multiple lazy-loaded routes, and our goal is to utilize a single language file for all ro ...

The request.files property in express-fileupload is consistently coming back as undefined

I am trying to achieve the task of uploading a file from my browser and sending it via POST to an Express.js application, which will then download the file using express-fileupload. Here is the client-side JavaScript code I have written so far: // Triggere ...

Create a visual representation of an item within a framework using an Angular directive

I am interested in using a directive to draw a triangle above a series of div elements. In my scenario, I have four squares and two values: charge and normal. The value of charge determines the color of the squares, while normal is used for drawing the t ...

Establishing the types of object properties prior to performing a destructuring assignment

Consider a scenario where a function is utilized to return an object with property types that can be inferred or explicitly provided: const myFn = (arg: number) => { return { a: 1 + arg, b: 'b' + arg, c: (() => { ...

It is not possible to transform Next.js into a Progressive Web App (P

Can someone assist me with PWA implementation? I tried running npm run build, but it was unsuccessful. > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cdbaacbface0abbfa2a3b98dfde3fce3fd">[email protected]</a> ...

Possible revision: "Dynamic property naming in TypeScript interface based on specified type"

The concept might seem complex, but here's the gist of it. I have a User interface that may or may not contain certain properties depending on where it is fetched from. For example, there are optional properties like role and client_details. export i ...

Calculate the sum of elements within an array

I've been attempting to calculate the sum of values in an array based on different levels, but so far I haven't had much success. Here are the data I'm working with (stored in a variable called totalByLevel): https://i.stack.imgur.com/rOP9 ...

Even after calling ChangeDetectorRef.detectChanges, the ngOnChanges hook failed to trigger

export class AppComponent implements OnInit, OnChanges { @Input() value: number; constructor( private cdr: ChangeDetectorRef ) {} ngOnInit() { of(1).subscribe(v => { this.value = v; console.log(1); this.cdr. ...

Implementing conditional properties in Typescript based on the value of another property

Is it possible to make a property required in a type based on the presence of another property? Here's an example: type Parent = { children?: Child[]; childrenIdSequence: string[]; // This property should only be required when `children` is prov ...

Convert C# delegate into TypeScript

Sample C# code snippet: enum myEnum { aa = 0, bb, cc, } public delegate void MyDelegate(myEnum _myEnum, params object[] _params); public Dictionary<myEnum , MyDelegate> dicMyDelegate = new Dictionary<myEnum , MyDelegate>(); publi ...

Issue with loading React Router custom props array but custom string works fine

I am facing an issue with my ReactTS-App where I pass a prop via Router-Dom-Props to another component. The problem arises when I try to use meal.food along with meal.name, or just meal.food alone - it doesn't work as expected. Uncaught TypeError: mea ...

Opening a new tab in Angular 6 from a component with freshly generated HTML (containing input data)

Hello everyone. I have a requirement where I need to open a new browser tab with formatted input data from a modal component. Here's an example code snippet that attempts to achieve this using ng-template: @Component({ template: '< ...

The variable 'FC' has been defined, however it is not being utilized. This issue has been flagged by eslint's no-unused

After running eslint, I encountered a warning stating that X is defined but never used for every type imported from react or react-native. For example, this warning appeared for FC and ViewProps (refer to the image below). Below is my .eslintrc.js configu ...

How can I easily move from a shared page to a specific page in Angular 8?

Just stepping into the world of front-end development, I have a scenario where my menu page offers 3 options to navigate: Go to Arena. Go to Dungeon. Go to Battleground. However, clicking on any of these options leads me to a common page for character p ...

The `process` variable is not recognized in a Vue/TypeScript component

I've encountered an issue trying to use .env variables in my Vue app. When I ran npm install process, the only syntax that didn't result in an error when importing was: import * as process from 'process'; Prior to this, I received the ...

Is it possible for decorators to invoke services within an Angular application?

Recently, I've delved into exploring decorators for TypeScript. Angular showcases a wide array of decorators like Components, Injectables, Directives, and more. I find myself frequently repeating a logic block that checks if a user is logged in and au ...

Enhance your website with unique and custom fonts using

I am utilizing this repository. How can I incorporate custom fonts into my project? I have created a folder named "fonts" within the assets directory and placed my fonts there. fonts.scss @font-face { font-family: 'Lato'; src: url('../ ...

What is the meaning of a "hook" in the world of HTML?

During a recent interview, a developer asked me about the "hooks" Angular uses with HTML. I admitted that I was not familiar with the term "hook," despite my extensive experience in web development over the past two decades. While I have some ideas of what ...

Mastering React children: A guide to correctly defining TypeScript types

I'm having trouble defining the children prop in TypeScript for a small React Component where the child is a function. class ClickOutside extends React.PureComponent<Props, {}> { render() { const { children } = this.props; return chi ...

Creating a dynamic multi-series line chart in D3 version 4 that incorporates data points with matching colors to their respective lines

In my attempt to enhance a multi-series line chart with D3 V4 in Angular-cli, I have encountered a challenge. Below is the code snippet of what I have been working on. var lookBookData = z.domain().map(function (name) { return { name: name, ...