Modifying $scope value within ngRepeat

Within a view, I am utilizing an ngRepeat loop where each item has a custom toggle control linked to a $scope variable in the following manner:

<a ng-repeat="item in items" ng-click="isOn = !isOn" ng-class="{on: isOn}">
   Toggle!
</a>

Upon click, the isOn variable toggles as expected, but interestingly, it only affects the specific item that was clicked. Considering that isOn is initialized in the controller as $scope.isOn = false, I would anticipate all items to be updated simultaneously.

However, if I toggle the variable using a controller method, then magically all the items get updated!

<a ng-repeat="item in items" ng-click="toggle()" ng-class="{on: isOn}">
   Toggle!
</a>

--- controller ---

$scope.toggle = function() {
    $scope.isOn = !$scope.isOn;
}

What could be the reasoning behind this behavior? Perhaps the use of ngRepeat generates distinct scopes for each item?

Answer №1

Absolutely, ngRepeat generates a unique scope for each item. For more information, check out the AngularJS ngRepeat documentation

The ngRepeat directive creates a new template for every item in a collection. Each template instance has its own scope...

Update: based on @doodeec's suggestion

Instead, you could switch the parent scope like this:

ng-click="$parent.isOn = !$parent.isOn"

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

Hmm, I seem to be encountering an error where res.sendStatus is not recognized as a function. What could be causing this

For the last few months, I have been immersed in Node.js/Express to create a REST Api. However, I've hit a roadblock with an async function in my controller.js file. The callback function is successfully receiving the client's request, but when i ...

Don't pay attention to jquery.change

Consider the code snippet below: $(".someSelector").change(function() { // Do something // Call function // console.log('test') }); An issue arises where this code is triggered twice: once when focus is lo ...

Does the DOMContentLoaded event fire only after all script tags have been downloaded?

Imagine this situation: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <title>Document</title> </head> <body> <script src="js/heavy_js_file.js" defer></script> &l ...

The md-items function is experiencing issues with updating the suggestion list correctly within the md-autocomplete feature of Angular Material

I am currently utilizing md-autocomplete, and I am facing an issue with the md-items not updating the response list correctly from the Service Host - Ajax Call. Here is the HTML Source Code: <md-autocomplete flex required md-input-name="autocomple ...

Ways to develop a dynamic HTML TransferBox featuring a custom attribute

I am in need of developing a customized transferbox using HTML, JavaScript, and JQuery. The user should be able to select from a list of options and associate them with attributes. This selection process should involve transferring the selected options be ...

Verify if the user possesses legitimate Jira REST API credentials

I am currently using the npm module jira-client to send API requests to my Jira system. I need a way to verify whether the user has valid credentials before proceeding. Depending on the validation result, I plan to either: Inform the user that their use ...

Arranging JSON data based on related properties in JavaScript

I have a JSON order with multiple products associated by vendorId, and I'm seeking to group them into separate arrays rather than having all of them listed together. How can I accomplish this? Here is my code: let order = { "product ...

Node package unable to undergo tree shaking process

My node module structure is similar to this: # color.js export var red = '#f00'; export var yellow = '#ff0'; export var blue = '#00f'; # images.js export var image1 = '<base64 string approximately ~ 500kb>&apos ...

Uploading profile images with AngularJS and Firebase

I am encountering an issue with my JavaScript image uploader. It successfully uploads an image and provides the image src, but I need to save this value in my FireBase DB for the current user that is being created. Initially, I attempted to output the img ...

What is the best way to obtain the chosen value or index from a Vue select element?

On the surface, this question seems straightforward, but there are some complexities to consider. The scenario involves: <select id="lstMash" @change="onChange()"><option value="valid">Valid</option><option value="warning">Warning& ...

JavaScript innerHTML not functioning properly when receiving a response from a servlet

Can someone help me troubleshoot the code below? I'm receiving a response from the servlet, but I can't seem to display it inside the div. Here is the response: lukas requests to be your friend &nbsp <button value="lukas"onclick="accfr(th ...

Angular 12 app does not have Express server searching for static files

Context I am in the process of transferring an Angular application to a GKE cluster. Unfortunately, due to company policy, the base docker image I am required to use does not support the installation of new software such as shell or Angular CLI commands l ...

SignalR 2.2 application encountering communication issues with client message reception

I am facing an issue in my application where clients are not receiving messages from the Hub when a user signs in. Here is my Hub class: public class GameHub : Hub { public async Task UserLoggedIn(string userName) { aw ...

What is the best way to fix multiple dropdown menus opening simultaneously in Vue.js?

I am working on an application that generates todo lists for tasks. These lists can be edited and deleted. I am trying to implement a dropdown menu in each list to provide options for updating or deleting the list individually. However, when I click on the ...

What is the best approach for updating data in a v-data-table dynamically?

I'm currently working on a node and electron application that utilizes vuetify to create a table (v-data-table) fetching data from an oracle database. The issue I'm facing is that even though the data changes based on the input value, the table f ...

The Appsheet algorithm determined the exact expiration date as 2 days from the specified date

In my Appsheet, I have two columns: Production Date and Expired Date. If the Production Date is 35 months before the Expired Date, how can I create a formula in Appsheet to calculate this? For example, if the Production Date is 01/10/2023, then the Expired ...

Is it possible for JavaScript within an <iframe> to access the parent DOM when

Is it possible for javascript in an iframe from a different domain to modify the parent's DOM? I need my iframed script to add new html elements to the parent page - any suggestions? Edit: One potential solution is using "Fragment ID Messaging" to co ...

Guide on adding the contents of non-empty <td> tags using Javascript or JQuery

My goal is to calculate the total sum of all the HTML content within <td> elements with the attribute name='gross_val'. Here are the <td> elements I am working with: <tr><td name="gross_val" align="right" title="gross_val1" ...

Setting up your own local Jquery Mobile Angular Adapter Todo app is easy and seamless

Currently, I'm in the process of familiarizing myself with the jQuery Mobile Angular adapter by configuring a local repository for the Todo app JSFiddle that is linked on the Github repository: http://jsfiddle.net/ocdemo/UM5Mr/ I am utilizing the "s ...

Combining Multiple Optional Async AJAX Calls

In my Angular 1.5.8 app, I have different views that require various combinations of the same 3 ajax requests. Some views need data from all three endpoints, while others only need data from one or two. To efficiently manage the retrieval of this data, I ...