Tips for updating the value of the most recently created div in an ng-repeat loop

Within my HTML document, the following code is present:

<div ng-repeat="selection in selections" 
   style="margin-left:{{marginLeft}}%;width:{{progress}}%;background-color:{{selection}}">

</div>

In my controller, I have implemented functions to update these values dynamically:

function calculateExpenditure(expenditure, budget){
    $scope.progress = (expenditure/budget) * 100;
    if(count !== 0){
        $scope.marginLeft += $scope.progress;
    }
}

An unexpected issue arises each time the calculateExpenditure function is executed, where the properties of margin-left, width, and background-color get updated on previously set div elements.

I am seeking guidance on how to instruct Angular to update the scope variables associated with these properties exclusively in the last instance created by the ng-repeat. Is there a more suitable directive other than ng-repeat that should be utilized?

Your assistance on this matter would be greatly valued.

Answer №1

Utilize the $last unique attribute within the ng-repeat element

 <div ng-repeat="item in list track by $index"
      style="{{($last?'margin-left:'+marginLeft+'%;
                    width:'+percentage+'%;':'')+
               'background-color:'+item}}">
 <br></div>

To learn more about the $last feature, refer to the AngularJS ngRepeat API Reference.


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

Tips on utilizing controllers within AngularJs directives?

In order to utilize a controller in my directive, what is the best way to access all controller functions within the directive? directive.js angular.module('App').directive('deleteButtons', function (prcDeleteFactory,$rootScope) { & ...

Task: Choose MySQL option and Retrieve JSON data

I have a question regarding implementing a function in a separate module file and calling it within a route to retrieve query data: function getSobre() { return new Promise((resolve, reject) => { db.query(`SELECT * FROM sobre ORDER BY cod ...

Utilizing event delegation for JavaScript addEventListener across multiple elements

How can I use event delegation with addEventListener on multiple elements? I want to trigger a click event on .node, including dynamically added elements. The code I have tried so far gives different results when clicking on .title, .image, or .subtitle. ...

Struggling with aligning and floating links to the right while crafting a custom navigation bar

I'm in the process of crafting a custom navigation bar for my website, with the intention of having my name prominently displayed on the far left while the links float to the far right. I've utilized CSS to style everything within the nav section ...

Modify the name format using an AngularJS directive

I've been struggling to understand how to effectively write AngularJS directives, even after reading various blogs. The issue I am facing is with an array of names in the format "lastname, firstname". My goal is to create a directive that will display ...

Activate a CSS class on an image upon hovering over a div (WordPress)

In my current setup, I have a loop that retrieves and displays posts using the following code: <div class="row" style="margin-bottom:50px;"> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <a h ...

Sending data between components in Angular can be achieved by using various methods. One common approach is to utilize

I am encountering an issue with a component named customers.component Below is the code from the customers.component.ts file: @Component({ selector: 'app-customer', templateUrl: './customer.component.html', styleUrls: ['./cu ...

What is causing the 'transitionend' event to trigger multiple times?

Currently, I'm honing my JavaScript skills by taking on the 30 days of JavaScript challenge. I'm puzzled by why the 'transitioned' event is triggered twice in the code snippet below. My CSS only contains one property called transform, ...

Enhancing jQuery UI Tabs with HoverIntent

I'm currently working on incorporating HoverIntent into my jQuery tabs, but I seem to be facing some issues. Below is the code snippet that I've been using. Any suggestions or tips would be greatly appreciated. Thank you! The code was sourced di ...

Attempting to eliminate an HTML element using JavaScript

Currently, I am working on creating an image rotator in JavaScript. The CSS fade animation I have applied only seems to work during element creation, so I am forced to remove the element on each loop iteration. The main issue I am encountering is related ...

When it comes to Shopify Schema, the section settings are functional within the <style> tag, whereas the block settings do not seem to have the same

While working on updating a section with multiple blocks, I encountered an unusual issue. My goal was to incorporate a new block into an existing section. The schema has been set up correctly, and everything is functioning as anticipated. However, the prob ...

How can I prevent a child div from activating the hover effect on its parent div?

I am currently exploring how to create a child element with position: absolute that is placed outside of its parent element without triggering the parent's :hover effect. Despite the parent having a hover effect, the child elements will still activat ...

The integration of a Kendo Grid with a Kendo Tooltip using an inline template is malfunctioning

My kendo Grid has a tooltip that displays details using a kendo tooltip. The template below works fine as an external template, but I am not sure if it can be passed as an inline template. Here is the code for the external template: <script id="ja ...

Optimizing the particle rendering speed for HTML5 <canvas> elements

Currently conducting an experiment to enhance the maximum particle count before frame-rates begin to decrease in HTML5 Canvas. Utilizing requestAnimationFrame and employing drawImage from a canvas as it appears to be the most efficient method for image re ...

Troubleshooting issue arising from transferring code from CodePen to a text editor

I've attempted to recreate a Codepen example in my HTML files on my computer, but it's not displaying correctly. While the static layout resembles what appears in Codepen, the background with the moving squares isn't functioning as expected ...

What is the best way to verify the application's authenticity when making AJAX requests?

I have the below Code written in AngularJs, but I need to ensure that the authenticated app is sending requests to the backend. How can I achieve this? resp.get Update = function () { //return $http.get(urlBase); return $http({ ...

Storing an Excel file with JavaScript: A comprehensive guide

I've been struggling to save an Excel file using Javascript, but I'm facing compatibility issues with different browsers. I initially tried using BASE64 along with data URL, which worked well in Chrome and Firefox but failed in IE and Safari. ne ...

Arranging divs within list items in a single row

My goal is to create a dropdown menu within an <li> element of <ul>. Here's what I have so far: HTML: <ul class="navigationTable"> <li>1</li> <li>2</li> <li><div id="comittee"> <div id= ...

initiate changes to the application's style from a component nested within the app

I'm looking to update the background color of the entire page, including the app-nav-bar, when I'm inside a child component. When I navigate away from that component, I want the default style to be restored. Any suggestions on how to achieve this ...

Centering text in a D3 donut chart

Struggling with centering text inside a d3 donut chart for a project. While trying to modify a piece of code, I find myself lost in the complexity of it. Despite my efforts, the text added to the center is not perfectly centered. I have attempted various ...