Steps to enable the submit button in angular

Here's the code snippet:

SampleComponent.html

<nz-radio-group formControlName="radiostatus" [(ngModel)]="radioValue" (ngModelChange)="onChangeStatus($event)">
    <label nz-radio nzValue="passed">Passed</label>
    <label nz-radio nzValue="failed">Failed</label>
</nz-radio-group>

<div *ngIf="radioValue ==='failed'>
   <textarea nz-input placeholder="Remarks" class="remarks-textarea" type="text" name="otherRemark"
          formControlName="otherRemark" [(ngModel)]="otherRemark"
          [nzAutosize]="{ minRows: 3, maxRows: 3 }"></textarea>
</div>
<button class="mr-1" nz-button nzType="primary" type="button" [disabled]="disableSubmitBtn()"
  [nzLoading]="formLoading" (click)="saveFormData()">
  <span translate>Submit</span>
</button>

SampleComponent.ts

disableSubmitBtn() {
   if (!this.otherRemark) {
      return true;
   }
}

How can I dynamically enable the submit button when "passed" is selected in Angular?

My requirement is that upon selecting "passed" on the radio button, the submit button should become enabled. If "failed" is selected, the button should be disabled until the remarks are filled in the textarea as per the disableSubmit function.

if (!this.otherRemark) {
    return true;
}

Thank you for your help!

Answer №1

Give this a try:

 <button [disabled]="radioValue === 'passed' ?  !(radioValue === 'passed') : !(radioValue === 'failed' && otherRemark)" class="mr-1" nz-button nzType="primary" type="button" 
    [nzLoading]="formLoading" (click)="saveFormData()">
      <span translate>Submit</span>
 </button>

Answer №2

When using the code [(ngModel)]="radioValue", be aware that the option value will be updated with ngModel.

Consider implementing the following approach:

disableSubmitBtn() {
  if (this.otherRemark.length == 0 || this.radioValue !== "passed") {
    return true;
  } else if(this.radioValue === "passed") {
    return false;      
  }
}

Answer №3

deactivateSubmitBtn() {
  if (this.radioValue === 'passed') {
    return false;
  } else if (!this.otherRemark) {
    return true;
  }
}

This information could be beneficial for you.

Answer №4

The most straightforward approach I usually take is something like this:

[disabled]="(radioValue !== 'passed') || (radioValue !=='failed' && !otherRemark)"

With this code, the button will be enabled as long as the radio value is not passed. Therefore, if the user selects any other value besides passed, it will become disabled.

Here's a complete HTML example:

<button [disabled]="(radioValue !== 'passed') || (radioValue !=='failed' && !otherRemark)" class="mr-1" nz-button nzType="primary" type="button" [nzLoading]="formLoading" (click)="saveFormData()">
      <span translate>Submit</span>
 </button>

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

I am experiencing slow load times for my Angular 2 app when first-time users access it, and I am seeking assistance in optimizing its speed

Below, you'll find a snippet from my app.ts file. I'm currently working with angular2, firebase, and typescript. I'm curious if the sluggish performance is due to the abundance of routes and injected files? The application functions smoot ...

Embarking on the journey of transitioning code from server-side to client-side

Currently, I am looking to transition the code behind section of my asp.net web forms application to client-side ajax or javascript - still deciding on which route to take. The main goal for this change is to ensure that the application remains functional ...

Implementing AJAX to dynamically insert content into div elements on the page

Currently facing a small issue with my AJAX implementation for creating comments on posts. The functionality is working well, but the problem arises when executing it in the index.html.erb view. The create.js.erb file locates the initial div labeled "comme ...

I need assistance with an issue on my Google Dev Console, as it keeps showing an error stating that ".getcontext is

Looking for some assistance here as my development console keeps displaying an error: Uncaught TypeError: canvas.getContext is not a function. Here is the problematic code snippet: `var canvas = document.createElement; var c = canvas.getContext("2d&qu ...

Distinguishing Between Model and View State Management in AngularJS

As I work on a complex single page application in Angular, I find myself needing to manage two types of data. First, there is the Model data which pertains to information retrieved from and sent to the backend API server. Second, there is ViewState data wh ...

Ensure there is a sufficient gap between the top and bottom icons within the Material-UI Drawer

I'm having difficulty articulating this, but I'd like to add two different sets of icons to the Drawer component. Set 1 should be displayed at the top in a standard column format, similar to the examples provided by them. Set 2 should go at the b ...

Filtering Array Elements in Vue JS: A Step-by-Step Guide

In my Vue Js 2.0 application, I am working on filtering elements in an array format. Here is the code snippet: const search = this.search_by_name.toLowerCase() const searchContact = this.search_by_contact.toLowerCase() return this.meetings .map(i => ...

Ways of retrieving Sveltekit session data in an endpoint

Is there a way to access a session in an endpoint using SvelteKit? I attempted the following with no success: import { get } from 'svelte/store'; import { getStores} from "$app/stores"; function getUser() { // <- execute this du ...

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 ...

What is the approach to constructing an observable that triggers numerous observables depending on the preceding outcome?

One of my endpoints returns { ids: [1, 2, 3, 45] }, while the other endpoint provides values for a given id like { id: 3, value: 30, active: true }. I am currently attempting to create an observable that will call the first endpoint and then, for each id r ...

The auto search feature seems to be malfunctioning after clicking the button

Despite my best efforts, I am still unable to resolve this issue. I have tried numerous links and code snippets, but I am encountering some difficulty in finding a solution. THE ISSUE: I have an input field with type 'Text' for searching employ ...

Retrieves MySQL SQL findings and converts them into a JSON structure

I'm in the process of integrating Typeahead.js into my website. The Typeahead.js plugin will retrieve data from a remote page that returns JSON, similar to: http://example.org/search?q=%QUERY Here's the PHP code I've written for my site: ...

Enhance the speed of filtering a large array of 4000+ objects in React for optimal performance

I am currently dealing with a component that generates an input of type text for filtering an array containing over 4000 objects. const { airports } = useContext(MainContext); const [airportListLocal, setAirportListLocal] = useState<Airport[]>(airp ...

Master the art of directing your attention to a list element with ease using the tab function and jQuery

I've been tasked with creating a questionnaire for a client. They want the current active question to be displayed at 100% opacity, while the inactive questions should be at 20% opacity. Currently, when the page loads, all questions are dimmed to 20% ...

Is it possible to implement a redirect within a Guard in Angular while utilizing asynchronous validation methods?

Within my Angular project, specifically version 6.x.x, I am working on implementing a Guard for certain routes that will redirect to '/login' if validation fails. The Guard relies on an asynchronous method within the AuthService for user validati ...

Performing sequential HTTP requests on an array using Angular 4 and RxJS

Looking to perform sequential HTTP posts for each item in an array, using RxJS flatMap without success. Essentially, I'm trying to achieve the following: Each item from the 'items' array should be sent through HTTP post one by one. thi ...

Is it possible to change the text of a scrollspy dropdown to a default text when it is not actively tracking any items?

I am new to Vue and currently implementing Bootstrap Vue Scrollspy (view example here). My sticky dropdown is tracking all referenced content and updates with the current section in view. You can check out my code sample here. Is there a way to set the d ...

What exactly is the significance of the code snippet "var data = jQuery(msg), script;" in JavaScript?

this snippet is extracted from a Google Chrome extension "search" == request.ajax && $.ajax({ url: request.url, type: "GET", dataType: "html" }).done(function(msg) { if (msg.indexOf("https://login.testabc.com/ ...

Repetitive series of HTTP requests within a looping structure

Within my AngularJS project, I encounter the need to execute a varying number of HTTP requests in sequence. To achieve this, I believe that utilizing a loop is necessary: for (let i = 0; i < $scope.entities.length; i++) { MyService.createFixedValue ...

Tips for showcasing messages in a .dust file with connect-flash and express-messages in a Node application

I am currently working with Nodejs, Expressjs, and Kraken. I have been trying to display a message when a product is added on the index page, but despite several attempts to configure it, the messages are still not appearing as expected. Below is my config ...