Utilizing ng-class within a nested ng-repeat loop

I'm currently working on a nested ng-repeat list feature, and my goal is to apply a specific class to the parent element when any item within the nested list undergoes modification.

Here's how the structure looks:

<div ng-repeat="pa in products" ng-class="{full: vari.basket>0}">
    <div ng-repeat="vari in pa.variacions">
       <input type="number" ng-model="vari.basket" name="basket" min="0" max="20" step="1" ng-init="vari.basket=0">
    </div>
</div>

In essence, I am aiming to assign the full class whenever the basket value of any 'vari' item exceeds 0.

Unfortunately, the implementation is not yielding the desired results. Can you point out what may be missing from my approach?

Answer №1

To dynamically assign a class to the parent element based on certain conditions, you can create a function in your controller.

<div ng-repeat="pa in products" ng-class="{full: isFull(pa.variacions)}">
    <div ng-repeat="vari in pa.variacions">
       <input type="number" ng-model="vari.basket" name="basket" min="0" max="20" step="1" ng-init="vari.basket=0">
    </div>
</div>

Define the following function in your controller:

$scope.isFull = function(variacions) {
  if (!Array.isArray(variacions)) {
    return false;
  }

  return variacions.find(vari => vari.basket > 0);
}

Answer №2

<div ng-repeat="pa in products" ng-class="{true: 'myFullClass'}[$ctrl.isProductBasketFull(pa)]">
    <div ng-repeat="vari in pa.variacions">
        <input type="number" ng-model="vari.basket" name="basket" min="0" max="20" step="1" ng-init="vari.basket=0">
    </div>
</div>

While this approach may not be the most efficient due to multiple loops being generated, one workaround could involve passing pa to a controller function that determines whether a vari basket is full, returning a boolean value based on the comparison.

In the controller:

$scope.isProductBasketFull = function(pa){
    for(var i=0,cnt=pa.length;i<cnt;i++)
        if(pa[i].basket === maxVal) // replace maxVal with your desired maximum value
            return true;
    return false;
};

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

Switch the custom audio control button on and off within a v-for loop

I have implemented a unique audio play/pause button in my Vue app for a list of audios. Here is how it looks: <div v-for="(post, p) in post_list"> ... ... ... <v-avatar v-if="!is_played" color="#663399" ...

Bring in a template in TypeScript with no need for require statement

Currently, I'm utilizing TypeScript along with Angular 1.5 component. In my scenario, I have a custom decorator in place through which I send templates via require function. The setup is functional; however, I am consistently receiving a tslint warnin ...

Show a pair of pictures every X hours using Javascript

Trying to create a code that displays different pairs of images each day, but struggling to make it select them randomly. Instead, the code currently just goes through the array one by one. I'm not great with Javascript and haven't found a way to ...

Fetching an image from an external API with NestJs: A beginner's guide

Recently, I started using a new framework for coding and found it quite amusing. However, I've run into a roadblock and now seek some assistance. The issue I'm facing is rather common in other frameworks or programming languages. I need to fetch ...

Having trouble creating a PDF from HTML

Having trouble with generating PDFs using various libraries as I keep encountering the following error: Fatal Error: spawn UNKNOWN The code snippet looks like this: mammoth.convertToHtml({ path: './backend/common/template.docx' } ...

Checking for valid usernames using Regex in JavaScript

I have attempted to use the following regex to validate against the criteria listed below, but it seems to fail in certain cases. Can you help me identify what might be wrong with my approach? Regular Expression: /[a-z]|\d|\_{4, 16}$/.test(user ...

Exploring XML with Jquery

Looking for assistance with parsing my XML Feed: Struggling to utilize JQuery to extract and display the data in HTML. Despite searching on Google, I haven't found a satisfactory solution yet. With numerous elements in my XML, it's proving to b ...

Save the content of the textbox on the webpage automatically, no need for a webservice or settimeout

Is there a way to save the text entered in a textbox on a webpage automatically, without relying on a webservice or the settimeout function? Any help would be greatly appreciated.Thank you in advance. ...

Update the Callbacks to trigger every time a user scrolls to a different section while using fullPage.js

I've added a fadeOut effect to certain sections, but I'm having trouble making them reappear after scrolling a second time. My knowledge of JavaScript is limited, so I'm unsure about the correct solution. How can I achieve this? $(documen ...

Is there a way to activate jQuery by simply clicking on a link?

I am trying to implement a method for dynamically loading a new set of HTML using jQuery. Can someone help me figure out how to bind this functionality to a click event? Here is the jQuery snippet I have: $("body.page-ready").load("photo.html .test> * ...

Attempting to modify the element's value with the help of selenium

I am facing an issue while trying to change the element through Selenium. Despite using send keys and execute_script, the value remains unchanged. I am attempting to set the value to 'R'. driver.find_element_by_name('wlw-select_key:{actionF ...

The function location.path("#/") does not seem to be functioning properly

I'm encountering an issue with my code where location.path("#/") is not functioning as expected. I have spent a considerable amount of time trying to troubleshoot the problem but haven't been able to pinpoint why it's not working. The basic ...

When buttons contain an image instead of text, event.target.value will be undefined

I'm facing an issue with two buttons that are almost identical, except one includes an image while the other has text content. I have added onClick event handlers to both of them. Oddly, the event.target.value for the image button is coming up as und ...

Transferring information among PHP web pages using a list generated on-the-fly

I am working with a PHP code that dynamically generates a list within a form using data from a database: echo '<form name="List" action="checkList.php" method="post">'; while($rows=mysqli_fetch_array($sql)) { echo "<input type='pas ...

Maintaining the state of a React component across page refreshes, with a preference for storing the

As a newcomer to React, I am utilizing localstorage in my React app to store certain data that is needed for page refreshes along with the useEffect() hook. My only concern is that I wish to find a way to conceal this particular data, as it seems I cannot ...

Using Backbone.js to dynamically filter a collection when a user clicks a specific element

update added more details about my progress so far. I'm currently in the process of developing an app that showcases the gists of members belonging to a specific organization, drawing inspiration from bl.ocks.org. My goal is to enable users to click ...

comprising an HTML element inside a div element without altering its appearance

I've created a function that wraps an HTML element with a <div>. The intention is to maintain the position and page layout intact, without causing any significant design changes (except for natural flow adjustments). Currently, the function appe ...

"Waiting for results with Node.js and Mongo DB is like trying to catch a

I am currently working on developing a Web Api using NodeJs and MongoDB. One issue I have encountered is that my await statements are not being awaited, leading to unexpected behavior in my code. Code async find_nearby_places(lng, lat, tag, maxDistanc ...

The weight of the Blender model is excessive

Seeking advice on optimizing loading times for my threeJS website. Currently experiencing long initial loading due to 4 models and textures. Check it out: Any suggestions on how to effectively reduce model size would be greatly appreciated! Thank you in ...

Is there an issue with this straightforward Javascript function for switching the CSS class of an HTML body?

I have been diligently working on this piece of code for a while now. I have carefully placed alerts to debug it and ensured that the flow is correct. The necessary styles are all in place. Initially, the body starts with the "styleBlack" class. When the ...