Updating a model within an ng-repeat directive from external sources

Within my controller, there is a repeater where each item has its own unique status:

<div ng-controller="...">
  <div ng-repeat"...">{{status}}</div>
</div>

Currently, changing the status within the repeater by using status = 'foo' works perfectly. However, I need to change this status from the controller in order to incorporate ajax calls and other functionalities. Despite my efforts to search online and consult with Google, I have been unable to find any information on accessing the scope of an individual repeated item.

This fiddle demonstrates the issue. When you click one of the buttons, it changes the status of the entire scope to clicked. My goal is to only change the color of the circle next to the button to blue without adding another value to the list for specific reasons.

Answer №1

This situation exemplifies prototypal inheritance. To assign a status in the current scope, you can utilize this.status.

You can view my demonstration on this topic by clicking here:

http://jsfiddle.net/BdcuS/

When the method is called, it operates within the context of the current scope, specifically the scope of ng-repeat.

I also recommend checking out this informative wiki page for further insight: https://github.com/angular/angular.js/wiki/Understanding-Scopes

Answer №2

Every individual item should be assigned its own distinct status attribute, instead of sharing a single one connected to the controller. Here is an example:

function barCtrl($scope, $timeout){
    // Each object in the list now has its own unique status property
    $scope.list = [{status: null}, {status: null}, {status: null}];

    $scope.setStatus = function(item, status){
        // Update only the status of that specific item
        item.status = status;

        //Simulate an ajax request
        $timeout(function(){
            item.status = null;
        }, 1000);
    }
}

View:

<div ng-repeat="item in list">
    <div class="circle {{item.status}}">

    </div>
    <button ng-click="setStatus(item, 'clicked')">click me</button>
</div>

Check out updated jsFiddle

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

What advantages does NextJS offer that set it apart from other frameworks that also provide Server Side Render solutions?

I'm diving into the world of NextJS and as I explore this topic, one burning question arises: "What is the necessity of utilizing NextJS?" From what I understand, NextJS excels in rendering pages from the server and is heavily reliant on ReactJS. Howe ...

Encountering a "Module not found" issue while attempting to utilize the Google APIs Node.js client

Attempting to incorporate the Google API Node.js client into a fresh React Web application. Here is my package.json: { "name": "onboarding", "version": "0.1.0", "private": true, "devDependencies": { "react-scripts": "0.8.4" }, "dependencie ...

Is it possible to add data in MongoDB without specifying a field name?

I have a couple of queries that revolve around the same concept: If I want to insert a new 'row' in MongoDB, can I do so by specifying the order of the fields? For instance, if my collection looks like items = { { name: "John", age: "28" ...

When using Vue3 along with Axios.post, the data is being serialized incorrectly

Goal: I need to send the data {"username": myuser, "password": mypswd} to an API endpoint in order to receive a token for further communication with the API. The following code snippets attempt to achieve this: // Attempt # 1 let re ...

React App with Material UI V1-beta Integration

I just installed the Create React App example from Material-UI.com. curl https://codeload.github.com/callemall/material-ui/tar.gz/v1-beta | tar -xz --strip=2 material-ui-1-beta/examples/create-react-app Upon installation, I encountered the following erro ...

Substitute the division

Can I make the old div change its position and move to the side when I add a new div? And can all the old divs be hidden when adding four new divs? This is for my full news website and I want this applied to all four pages. First page: https://i.sstatic. ...

"Learn how to create a scrolling div using a combination of CSS and JavaScript with absolute and relative

After relying solely on "pre-made" components like Mui or TailWind, I decided to create a component using only CSS and maybe JavaScript. However, I encountered some difficulties when attempting to position a div inside an image using relative and absolute ...

Creating a new object in an empty array within my profile model (mongodb/mongoose) is simple. Just follow these steps to successfully add a

Presenting my Profile model: const ProfileSchema = new mongoose.Schema({ user: { type: mongoose.Schema.Types.ObjectId, ref: "User", }, company: String, website: String, location: String, status: { type: String, require ...

Analyzing the current time against a user-inputted time using Javascript

Looking at this html and javascript code, the goal is to compare an input time with the current time. If the input time is less than 2 hours, "Less time" should be displayed in the label; if it's more than 2 hours, then "sufficient time" should appear ...

Limiting the number of checkboxes selected in a Checkbox Group based on

I am working on a checkboxGroupInput that has 4 options (denoted as A, B, C, D). My goal is to restrict the selection to only 2 choices. The user should be able to pick a 3rd option. In this scenario, only the newly selected (3rd) and previously selec ...

Converting multipart/form-data to multipart/mixed: A step-by-step guide

Looking for guidance on utilizing multipart/mixed content-type in the document insert REST api (POST /v1/documents) rather than multipart/form-data. Can someone point me to a resource or example that demonstrates this? Thank you. For context, I am using A ...

Is there a way to pass around jest mocks across numerous tests?

In my test scenarios, I've created a mock version of the aws-sdk, which is functioning perfectly: jest.mock("aws-sdk", () => { return { Credentials: jest.fn().mockImplementation(() => ({})), Config: jest.fn().mockImplementati ...

My attempts to load the .mtl and .obj files using THREE.js have been unsuccessful

I've been working on creating a 3D model viewer using three.js, but I'm encountering issues with loading .mtl and .obj files. Despite following a tutorial at , the only model that loads successfully is the female one. Here is the code snippet I ...

Error: The request does not have the 'Access-Control-Allow-Origin' header

As a beginner in post requests, I've been encountering an error when attempting to make a post request. Despite searching for solutions, the answers are too complex for me to grasp how to adjust my code to resolve it. var url = 'http://unturnedb ...

Save a document to the file system and retrieve it using Express

Can res.download() be used after writing a file to the filesystem? router.get('/exportjson', (req, res, next) => { let json = `{"@dope":[{"set":"","val":"200"}],"comment":"comment","folderType":"window"}` const file = `${__dirname}/upload-f ...

Inconsistencies observed during the native JSON import process in JavaScript

Encountered a strange behavior when loading a JSON file into JavaScript while working on a React project. Seeking an explanation and guidance on properly accessing data from the JSON data store. The JSON file contains product data: { "product ...

Tips for including a line within a circular canvas

My progress bar crafted with css, javascript and html looks great (left image). However, I'm facing a challenge in adding white color dividers to enhance the appearance of the progress bar as shown in the image on the right. Despite my various attemp ...

Is there a way to adjust the size of text to perfectly fit within a fixed size div?

I find this scenario quite common, but I haven't come across any solutions for it: Let's say there is a fixed-width div that displays dynamically changing numbers. How can we adjust the font size so that larger numbers fit well within the fixed ...

Adding elements to a roster

Can anyone assist me with appending data from a csv file to an unordered HTML list? The csv file I have contains both single and grouped data for the list, as seen below: Test Group Name A,Test Link Name 1,Test URL 1 Test Group Name A,Test Link Name 2,Tes ...

Watch the video and follow along with your mouse using jQuery

Wouldn't it be cool to have a video play and follow your mouse pointer when you hover over the red box? And then once you move away, the video stops and disappears. Check out my progress so far: jsfiddle Here's the jQuery code I've used: $ ...