Is it possible to customize the appearance of elements that are disabled using the [disabled] attribute in Angular 8?

I currently have a button on my website that triggers a form:

<button (click)="create()" *ngIf="data.new" [disabled]="!dataForm.valid" color="primary" mat-raised-button>
   Go
</button>

Under specific conditions, this button disables all fields.

Is there a way to use CSS to style the form only when it is affected by the [disabled] attribute?

Answer №1

@Eudz provided a great approach to tackling the issue. However, if you want to apply a style globally, such as for an invalid input box, you can also use CSS targeting. Angular automatically adds classes to form inputs like ng-invalid, ng-valid, ng-pristine, etc.

You can then target these classes with CSS.

For instance:

input.ng-invalid {
    border: 1px solid red;
}

Answer №2

A great example of customization is when you need to style an input element based on the validity of a form:

<input type="text" [class.border-danger]="!dataForm.valid">

Alternatively, you can do:

<input type="text" [class.border-danger]="dataForm.invalid">

The condition !dataForm.valid determines whether the button should be disabled.

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

Attempting to start an Angular project using NG NEW constantly fails nowadays - always ends with error code EPERM

Can Angular still be considered a reliable framework when pervasive errors and bugs persist for extended periods without any clear resolution documented? .... 24695 silly saveTree | +-- <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cf ...

Issue with Left Alignment of Tabs in Material-UI

As of now, material-ui's latest version does not provide support for aligning tabs to the left in the component. However, I came across a workaround on this GitHub page I have implemented the same workaround, and you can view it here: Unfortunately, ...

Are the frameworks Vue, Angular, and React known for

During a conversation, I came across an interesting viewpoint criticizing popular frameworks such as Angular, Vue, and React. It was argued that these frameworks have a significant disadvantage: apart from the API part that interacts with the server's ...

Guide on how to log out in asp.net using an HTML button

I have designed a master page that features a "sign out" button. Can someone guide me on how to implement this button for logging out of the current session? Below is the code for the sign out button: <a href="#" class="btn btn-default btn-flat">Si ...

Display resize grip when hovering

Is there a way to make elements resizable using resize: both, but only show the corner handle when hovering over the element? https://i.sstatic.net/cGIYf.png I am looking for a solution to display that specific handle only on hover. ...

Pop-up notification badge using AJAX technology

Is there a way to implement a notification number badge using Ajax, similar to what Facebook does? I can't provide an image here, but most people are probably familiar with the concept. It's like when you sign into Facebook and see a small number ...

The DOMException occurred when attempting to run the 'querySelector' function on the 'Document' object

Currently, I am engaged in a project that was initiated with bootstrap version 4.3.1. I have a keen interest in both JavaScript and HTML coding. <a class="dropdown-item" href="{{ route('user.panel') }}"> User panel </a& ...

Encountering the "ERROR TypeError: Cannot read property 'id' of undefined" with Angular 4 FormArray

I have a specific requirement where checkboxes need to be added dynamically on my page. Currently, I am utilizing the Angular Reactive Forms method. The browser console displays the following error, even though the checkboxes are successfully added afterwa ...

My attempts at locating child elements using XPATH have been unsuccessful in finding an exact match

I have a specific requirement to input a string into the webdriver and then compare it with the label of a radio button. <input type="radio" onclick="Element.show('indicator_radio_term_190');render_selected_term('190','1', ...

What is the best way to allow a user to direct their custom domain to a specific route within a client-side Angular application?

I am developing an Angular application with a Firebase backend that allows users to create profile pages. I am looking to enable users to redirect their custom domains to their profile page within the app. I am unsure of where to begin or what resources ...

How come the changes in my react component's CSS are not updating in real-time?

After integrating redux into my create-react-app project, I've been struggling to implement a navigator that highlights the active "page link." To achieve this functionality, I am using a combination of react hooks (to maintain the current page state) ...

Load information from a JavaScript object when the user clicks dynamically

My challenge involves utilizing a JavaScript object that contains video information such as title and source URL. Within my HTML, I have image placeholders and the goal is to trigger a modal pop-up (using Twitter Bootstrap modal) of the specific video when ...

Is it possible to convert a blob to an image file using the FileReader in HTML

client side code <head> <script> var reader = new FileReader(); var objVal; var image = new Image(); reader.onload = function(e) { document.getElementById('propertyImg').setAttribute('src', e.target.result); }; fun ...

Is it advisable to adjust the type of input control according to the drop down choice made?

I am currently working on creating an edit screen for users to update rows of data. One of the fields is a dropdown menu, while another field is an input labeled 'value'. Depending on the selection in the dropdown menu, I need to display differen ...

Expanding CSS styles on hover

When my mouse pointer moves out of the red color box, it still triggers the hover function. However, I only want the hover effect to work within the red color box and hide the menu when it's outside the box. This is the source code from JSfiddle: htt ...

Tips for activating a sticky navigation button by scrolling down slightly

Currently in the process of developing a website using React and focusing on optimizing the mobile version first. One feature I am working on is to have a sticky navigation bar that remains at the top after scrolling down. In the attached picture, there is ...

Vanilla JavaScript error: Unable to access property

I am working on implementing a header with a logo and navigation that includes a menu toggle link for smaller viewports. My goal is to achieve this using Vanilla JS instead of jQuery. However, when I click on the menu toggle link, I encounter the followin ...

Tips for adding style to a child component from a parent component with multiple child components

I am faced with a situation where I have a child component in the form of a spinner that I need to display in two different places, each with its own unique style. In order to achieve this, I have attempted the following approach: my-loading-overlay ::ng-d ...

Struggling to make fadeIn() function properly in conjunction with addClass

I've been struggling with this issue for quite some time now and I just can't seem to make it work. The JavaScript I'm working on involves using addClass and removeClass to display and hide a submenu element. While the addclass and removeCla ...

Having difficulty configuring Compass in WebStorm

Trying to integrate Compass into an existing WebStorm project has been challenging for me as the CSS files are not linking properly. Despite my efforts to modify the relative links, I have not yet managed to resolve the issue. The folders and config.rb we ...