Angular 6 allows for the use of radio buttons to dynamically enable or disable corresponding fields

Here is the HTML code for a row containing radio button selections:

<div class="form-row">
    <div class="col-md-3 mb-3">
        <div class = "form-group form-inline col-md-12 mb-3">
            <div class="form-check form-check-inline">
                <input class="form-check-input" type="radio" formControlName = "flag"  value = "Y" (ngModelChange)="radioValueCheck(1)" >
                <label class="form-check-label">Yes</label>
            </div>
            <div class="form-check form-check-inline">
                <input class="form-check-input" type="radio" formControlName = "flag" value = "N" (ngModelChange)="radioValueCheck(2)" >
                <label class="form-check-label">No</label>
            </div>
        </div> 
    </div>
    <div class="col-md-9 mb-3">
        <div class="form-row">
            <div class="col-md-6 mb-3">
                <label> Date</label>
                <div class="input-group">
                    <input class="form-control" placeholder="mm/dd/yyyy" formControlName="date" ngbDatepicker #date="ngbDatepicker" [attr.disabled]="showInputFlg ? 'disabled' : null">
                    <div class="input-group-append">
                        <button class="btn btn-secondary" (click)="date.toggle()" type="button">
                            <i class="fa fa-calendar"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

In my TypeScript file, I have a function called radioValueCheck(x) to set showInputFlg to true or false based on which radio button is selected.

radioValueCheck(x) {
    if(x == 1){
      this.showInputFlg=true;
    }
    else{
      this.showInputFlg=false;
    }
    }

I am trying to enable/disable the date field based on the radio button selection. How can I achieve this?

Answer №1

One way to handle radio buttons in your component is by creating a variable that links to the input:

public showDateInput = false;

radioValueCheck(x) {
    this.showDateInput = (x === 1);
}

You can then use [disabled] in your DOM :

<div class="form-check form-check-inline">
    <input class="form-check-input" type="radio" [checked]="showDateInput" id = "Y" (click)="radioValueCheck(1)">
    <label for="Y" class="form-check-label">Yes</label>
</div>
<div class="form-check form-check-inline">
    <input class="form-check-input" [checked]="!showDateInput" type="radio" id = "N" (click)="radioValueCheck(2)">
    <label for="N" class="form-check-label">No</label>
</div>

<input 
    class="form-control" 
    placeholder="mm/dd/yyyy"
    [disabled]="!showDateInput">

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

import component dynamically from object in Next.js

Currently, I have a collection of components that I am aiming to dynamically import using next/dynamic. I'm curious if this is achievable. Here's the object in interest: // IconComponents.tsx import { Tick, Star } from 'components ...

Is there a way for me to retrieve the value that has been set within the cy.get() function in Cypress using Typescript?

Is there a way to retrieve the value of the getLength function without it returning undefined? How can I access the value in this case? Here is my code snippet: const verifyValue = () => { const selector = 'nz-option-container nz-option-item&apo ...

Jasmine was unsuccessful in detecting a exported function being invoked by another function

In my code, I've created 2 helper functions where one is a shortcut to the other. I need to verify in my test that the shortcut function is actually calling the main function. Both functions are located in the same file: export function test1(param1, ...

Obtain a union type in TypeScript based on the values of a field within another union type

Is it feasible in Typescript to derive a union type from the values of a field within another union type? type MyUnionType = | { foo: 'a', bar: 1 } | { foo: 'b', bar: 2 } | { foo: 'c', bar: 3 } // Is there an automati ...

Issues arise due to data inconsistency stemming from the combination of a for loop and .map() function within the BACK4APP

I seem to be facing a challenge with data consistency caused by the for (const object of results) {} loop in the Sandbox Link at line41. The issue is that the results are displayed as a single result after using the .map() method. However, when I perform a ...

Utilizing TypeScript for dynamic invocation of chalk

In my TypeScript code, I am trying to dynamically call the chalk method. Here is an example of what I have: import chalk from 'chalk'; const color: string = "red"; const message: string = "My Title"; const light: boolean = fa ...

TypeScript declaration: discovering modules and defining namespaces

I'm currently in the process of creating a declaration file for h3. For guidance, you can refer to the function available here. Initially, I'm unsure of how typescript identifies declaration files. It seems to detect my declaration when placed ...

Exploring ways to conduct a thorough scan of object values, inclusive of nested arrays

My goal is to extract all values from an object. This object also includes arrays, and those arrays contain objects that in turn can have arrays. function iterate(obj) { Object.keys(obj).forEach(key => { console.log(`key: ${key}, value: ${o ...

What causes the createResource error to become undefined when it is refetched before being properly set?

How can I access and display the error property for a createResource? In this scenario, why is the error initially set to undefined before it is thrown? The logging shows that it first displays undefined for the error before eventually getting to line er ...

Definitions provided for Redux (Toolkit) store including preloadedState

I'm currently working on setting up typings for configuring a Redux store with a preloaded state. While following the Redux Toolkit TypeScript quick start guide, I came across this example: import { configureStore } from '@reduxjs/toolkit' ...

MUI: Transforming the uncontrolled value state of Select into a controlled one with a new component

I'm attempting to develop an edit form for modifying data fetched from a database based on its ID. Here is what I have tried: import React, {FormEvent, useEffect, useState} from "react"; import TextField from "@material-ui/core/ ...

Using private members to create getter and setter in TypeScript

Recently, I developed a unique auto getter and setter in JavaScript which you can view here. However, I am currently unsure of how to implement this functionality in TypeScript. I am interested in creating an Object Oriented version of this feature if it ...

Accessing external data in Angular outside of a subscription method for an observable

I am struggling to access data outside of my method using .subscribe This is the Service code that is functioning correctly: getSessionTracker(): Observable<ISessionTracker[]> { return this.http.get(this._url) .map((res: Response) => ...

Error in TypeScript while running the command "tsd install jquery" - the identifier "document" could not be found

Currently, I am facing an issue with importing jQuery into my TypeScript project. In order to achieve this, I executed the command tsd install jquery --save, which generated a jquery.d.ts file and added /// <reference path="jquery/jquery.d.ts" /> to ...

Is there a way to stop observing an Observable or Subject after its first emission while setting up the observer?

Currently, I am in the process of implementing dialogs using Angular 9 and I encountered a scenario where I need to unsubscribe from a Subject after its first emission. Initially, my approach was to use the take(1) or first() operators to transform the Su ...

Creating Instances of Variables Within a Class

Currently, I am working on a project using Ionic and Angular. I have come across various ways of instantiating variables and I'm unsure about the implications of each method. Here are three scenarios that confuse me: export class someClass { myVaria ...

The error message you are encountering is: "Error: Unable to find function axios

Can't figure out why I'm encountering this error message: TypeError: axios.get is not functioning properly 4 | 5 | export const getTotalPayout = async (userId: string) => { > 6 | const response = await axios.get(`${endpoint}ge ...

Troubleshooting problem with refreshing URL on "ionic serve" mode

After transitioning my project from Ionic 2 to Ionic 3, I've encountered an issue with ionic serve and the rebuilding process. Initially, when I build the project, everything functions as expected. However, I've noticed that the URL in the brows ...

Leveraging the Recyclability Aspect of a ReactJS Modal

Looking for a way to make a modal dynamic without duplicating too much code. Any suggestions on how to achieve this? I've managed to separate the state from the layout using render props. interface State { open: boolean; } interface InjectedMod ...

Struggling to integrate a JavaScript sdk with an Angular2 application due to missing dependencies

I've been struggling to incorporate the Magic: The Gathering SDK library into my Angular2 application. I've tried various methods, but nothing seems to work seamlessly. When I attempt to import the library using TypeScript like this: import { } ...