AngularJS e-commerce application: Deactivate button if quantity falls below 2

I am currently facing an issue with disabling the 'add' ('+') and 'subtract' ('-') buttons. The functionality should be as follows:

  • quantity == 1 -> '-' button is disabled
  • quantity > 1 && quantity < 15 -> both '-' and '+' buttons are enabled
  • quantity >= 15 -> '+' button is disabled

If you have any suggestions or tips to assist me in resolving this issue, please feel free to share.

You can access my entire application on jsfiddle: https://jsfiddle.net/scgsc7or/23/

I attempted to address it using jQuery's addClass and removeClass, but unfortunately, when integrated into my $scope.addQuantity function, it impacts all buttons rather than just the specific one involved.

Thank you in advance for any insights provided.

Answer №1

Utilize the ng-disabled directive.

Simply insert your expression into the ng-disabled attribute.

 var app = angular.module('app', []);

 app.controller('MainController', ['$scope', '$http', '$log',
   function($scope, $http, $log) {

     // Controller logic here

   }
 ]);
.container {
  // CSS styles here
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

<div ng-app='app' class="container">

  <div class="content" ng-controller="MainController">


    <div class="col-xs-6">
      <ul class="list-group itemList">
        <li class="list-group-item" ng-hide="product.isHidden" ng-repeat="(id, product) in drinks" ng-click="addToShoppingList(id)">
          <strong>{{ product.name }}</strong> - {{ product.price | currency }}
        </li>
      </ul>
    </div>

    <div class="col-xs-6 rightPane">
      // Shopping cart UI elements here
    </div>


  </div>

</div>

For a live example, visit this fiddle: https://jsfiddle.net/scgsc7or/24/

Answer №2

To make changes to the current ng-repeat scope, it is recommended to use ng-class instead of Jquery.

For the - button:

<button ng-click="addQuantity(id)"
              class="btn btn-xs btn-info pull-right addBtn" 
              ng-class="{disable:product.quantity<2}" >
                        +
                    </button>

For the + button:

   ng-class = "{disable:product.quantity>14}"

CSS:

   .disable{
        opacity:0.2;
        pointer-events:none;
    }

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

Working with Parse.com's REST API: Implementing skip and limit queries using JavaScript

Currently, I am in the process of retrieving data using Parse through the REST API in Javascript. Everything is working fine until I attempt to include a limit and an offset ("skip"). Here, you can find the documentation for using parameters with a REST ...

Using AngularJS ngRepeat to dynamically render a series of iframes by tracking element changes

Currently, I am displaying a series of TinyMCE wysiwygs by utilizing the ng-repeat directive: <div ng-repeat="widget in wigdets"> <textarea wysiwyg></textarea> <!-- This is a directive to initialize TinyMCE --> </div> Upo ...

Adding a clear field icon to an input field: Step-by-step guide

When using AngularJS 1.3, a standard input field with type="date" comes with a convenient "Clear field" icon as shown here. Is there a method to incorporate this same "Clear field" icon into an input field with type="text"? ...

What steps can I take to troubleshoot the cause of my browser freezing when I try to navigate to a different webpage

Within this div, users can click on the following code: <div id="generate2_pos" onclick="damperdesign_payload();" class="button">Generate P-Spectra</div> Upon clicking, the damperdesign_payload() function is triggered, leading to a link to an ...

When taking a vertical picture on my iPhone, my Vue component does not function properly

Having trouble with displaying recently uploaded images on a form. If the image is taken from a phone, it may have an exif property that browsers do not handle correctly, causing the picture to appear flipped or sideways. Upside down photo To fix this is ...

Looking to switch the markers from circles to icons in the GeoChart component of react-google-charts

During my personal project, I came across the react-google-charts library and discovered a method to add markers to the map. <Chart width={"200%"} height={"100%"} chartType={"GeoChart"} data={geoData} rootProps={{ "data-testid": "1" ...

The Chocolat.js Lightbox plugin is experiencing issues with jQuery as it is unable to detect the

I am in the process of integrating chocolat.js into a basic website. An issue I encountered was that the thumbnail was directly processing the anchor link, which resulted in the image opening in a new window instead of launching it in a modal on the screen ...

Leveraging .Net ConfigurationManager.AppSettings in Cascading Style Sheets

I am facing an issue where I have a specific color key in my AppSettings for the company's brand color, which needs to be applied to a CSS class. The challenge is how to incorporate this key into my stylesheet. Is there a way to access the Configurati ...

What is the Alternative for Angular2's $location.search() for Setting Query Parameters?

My website includes various filters that need to be reflected in the URL so that users can easily save and return to the current page by copying or bookmarking it. In Angular 1, I used $location.search(name, value) to update the search parameters easily. ...

The PrimeNG table component in Angular is having trouble maintaining a fixed width

I am facing an issue with fixing the width of the first column in a primeNG table. Despite inspecting the element and confirming that it is displaying correctly, my CSS adjustments are being overridden. Essentially, I am seeking a CSS solution where, rega ...

What results can be expected from assessing the statements provided in the following code block? {javascript}

Hello everyone, I'm currently in the early stages of learning about Javascript and programming in general. As I prepare for a test, I've come across a question that has me feeling stuck. I need help evaluating the following statements to ...

Update an element in array1 using a value from array2 when a link is clicked, efficiently implemented with AngularJS

Just starting out with Angularjs and decided to create a memory card game as a way to reinforce my understanding from tutorials. The Game Basics When the user clicks on a link for a card, it flips over revealing its value. In the JavaScript file, there ...

Having trouble with elFinder? Encountering an error stating "Undefined is not a function" when attempting to

I am currently in the process of integrating the elFinder file manager into my project. Here is a summary of what I have accomplished so far. I have included the following files: $arr_css = array( "main.css", "jquery-ui.cs ...

What is the best way to navigate down a page using the <a> tag?

I'm working on creating a mini wiki page on my website that will have a table of contents at the top. Users can click on a link in the table of contents and it will automatically scroll down to the relevant section of the page. I know this involves us ...

Resources for ExpressJS

New to ExpressJS and trying to figure out how to reference images stored in the /public/images/ directory in my /public/stylesheets/styles.css The code snippet below doesn't seem to be working for me: .home-bg { background-image:url('../ima ...

Error: Material-UI prop type validation failed - Please specify either `children`, `image`, `src`, or `component` prop

Despite having an image prop in CardMedia, I kept encountering this error. Here is a snippet of the code: Error description: const Post = ({ post, setter }) => { const classes = useStyles(); return( <Card className={classes.card} ...

Dreamweaver seems to struggle to properly execute the identical code

There are times when I transfer javascript code from jsfiddle to Dreamweaver and find myself frustrated. Two documents with seemingly identical javascript don't function the same way. In document A, I have this code: <script> alert('test& ...

Unable to open a modal in Angular UI after closing the initial modal

Hey there! I've been attempting to open a modal after closing the first one, but I'm encountering an issue where the second modal appears to be opened but is actually not. I'm struggling to understand what's causing this problem. Could ...

Tips on revealing div elements using a slide-down animation exclusively with JavaScript

I am looking to display a div with a slide-up effect using JavaScript (not jQuery) when clicked. Here is the HTML code I have: <div class="title-box"><a href="#">show text</a></div> <div class="box"><span class="activity- ...

The android web browser and its interactive virtual keyboard

I am currently developing an HTML5 application using PhoneGap version 2.5.0. One issue I have encountered is related to the virtual keyboard behavior on different parts of the app. On the search page, the keyboard appears without obstructing the view, all ...