optimize css on dropdown buttons that uses ngIf

ng if selected option angular 6
ng-if not equal
ngif not working
ng-if expression
ng-if else angularjs
ngif angular 8 example
ngif button clicked
ng-if multiple conditions angular 4

I have a dropdown that at the moment exists of one button. I would like this button to be disabled and that the color changes when it is disabled. I created this behaviour already with the code underneath.

Can't this be written cleaner? Because now I actually create 2 buttons but only one is seen.. I have tried this but I get stuck when adding the css background-color in the button code.

<div class="block-options">
 <div class="dropdown">
  <!-- Options -->
   <button type="button" class="btn-block-option dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="si si-settings"></i></button>
    <div class="dropdown-menu dropdown-menu-right" x-placement="bottom-end">
    <!-- Disable buttons based on selected packages -->
     <div *ngIf="checkIfValidVersion()">
       <button class="dropdown-item js-swal-confirm" (click)="createMajors()">Create major version</button>
     </div>
     <div *ngIf="!checkIfValidVersion()">
       <button [disabled]="!checkIfValidVersion()" style = "background-color:grey; color:black" class="dropdown-item js-swal-confirm">Create major version</button>
     </div>
   </div>
 </div>
</div>

Ok, what you have to do is write a styling class for your button that will take effect if the button is disabled

Something similar to this will do the trick:

.my-button:disabled {
  background-color: grey;
  color: black
}

and your HTML will look like this:

<div class="block-options">
  <div class="dropdown">
    <!-- Options -->
    <button type="button" class="btn-block-option dropdown-toggle" data-toggle="dropdown" aria-haspopup="true"
      aria-expanded="false"><i class="si si-settings"></i></button>
    <div class="dropdown-menu dropdown-menu-right" x-placement="bottom-end">
      <!-- Disable buttons based on selected packages -->
      <button class="dropdown-item js-swal-confirm my-button" (click)="createMajors()"
        [disabled]="!checkIfValidVersion()">Create major version</button>
    </div>
  </div>
</div>

Related topic here

Optimizing ngFor in Angular2 with dropdowns in child components , If I wasn't using Angular2, I would probably just create a single menu and then *ngIf="mappingDropdownValues"> <div class="dropdown-item" *ngFor="let val of However, the static CSS version of the dropdown toggles the dropdown by  Tip: If you want the width of the dropdown content to be as wide as the dropdown button, set the width to 100% (and overflow:auto to enable scroll on small screens). Instead of using a border, we have used the CSS box-shadow property to make the dropdown menu look like a "card".


<button class="dropdown-item js-swal-confirm" [class.disabledStyle] ="!checkIfValidVersion()"> Create major version </button>

The above code will add a class name "disabledStyle" to the existing class if the function returns true.

disabledStyle {
 background: grey;
}

fix(dropdown): support ngIf directive in templates of dropdown , When specifying a template-url for the dropdown-menu, the use of jQuery's replace starts to create an expanding shadow from the dropdown-menu CSS class. Then here is a best jquery dropdown menu plugin you can use. It has tons of customization features & unlimited possibilities to make any kind of dropdown & mega menus. It supports image gallery, multi-level dropdown menu items, unlimited child items, slider with caption, signup/signin, subscribe/contact form & much much more you can do with it.


You are duplicating the code. You just require one line which is already there. Based on condition it will put disable attribute in your button. Do not put css code at button level.

<button [disabled]="!checkIfValidVersion()" class="dropdown-item js-swal-confirm">Create major version</button>

Now put css at button level for disable scenario:

button[disabled=disabled], button:disabled {
   background-color:grey;
   color:black;}

This is most cleaner way.

Angular ng-if Directive, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  How to use *ngIf else in Angular. In this tutorial, we are going to take a look at the ngIf directive. We will discover how we can use it to show or hide parts of our angular application. Also, we will find out how the ngIf directive differs from using the "hidden" attribute.


Angular ng-options Directive, The ng-options directive uses an array to fill the dropdown list. In many cases it would be easier to use the ng-repeat directive, but you have more flexibility when​  As per my code when user is clicking on clear button the form is getting cleared but still values are displayed on the dropdown list. Here I need to clear form and also the values from all kind of input field. Source: New feed Source Url Can not clear the drop down value on button click using Angular material


Angular ng-selected Directive, and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML. A common UI pattern that we see on the web are dropdown menus. They’re used to display related information in pieces, without overwhelming the user with buttons, text, and options. Somewhere that we see these a lot is inside of headers or navigation areas on websites. Let’s see if we can make one of these menus with CSS alone.


Angular ng-disabled Directive, and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML. In this Angular select dropdown example, we will use the Angular Material library to construct UI/UX. Angular Material components will help us in building attractive UI and UX, consistent, and functional web pages and web applications while keeping to modern web design principles like browser portability and compatibility, device independence, and graceful degradation.