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

Add an element to the input field

Two input buttons are available for users to upload files. <input type="file" id="fileUpload" name="files" multiple><br/> <div id="selectedFiles"></div> The selected files will be appende ...

What is the best way to create dynamic transparency based on cursor position?

Is there a way to create an animation like the one on https://meetwalter.com, where the transparency changes with the cursor's position? I previously attempted a similar implementation using CSS, but it seems that the website accomplishes this effect ...

Tips for revealing a hidden HTML tag

I am currently trying to validate the content within #textd to ensure it is not empty and contains more than 150 characters. If it meets these conditions, I need to transfer the content to another webpage; otherwise, display an error message based on the c ...

Expanding the capabilities of search and replace in Javascript is imperative for enhancing its

I have developed a search and replace function. How can I enhance it by adding a comment or alert to describe the pattern and incorporating a functional input box? Any suggestions are welcome! <html> <head> <title> Search & Replace ...

How to adjust cell alignment in Handsontable

Handsontable showcases cell alignment options in the align cell demo: Horizontal Left Center Right Justify Vertical Top Middle Bottom Displayed below is a screenshot: To retrieve data, utilize the following code snippet: $('#table-wrapper&ap ...

Can you display a popup on a leaflet map from beyond its boundaries?

Utilizing leaflet maps, I have implemented a functionality where clicking on the map opens a popup at a specified location and centers on that position. The code for this is as follows: var popup = L.popup(); function onMapClick(e) { popup . ...

How to Delete Decimal Points from a String Using Regular Expressions

I am searching for a way in JavaScript, such as REGEX, to eliminate decimal numbers from a string. Here is an example input: Mazda 6 2.0 Skyactiv-G Wagon 2018 - Startstop.sk - TEST Mercedes C63 AMG Sound REVVING 6,3l V8 And here is the expected output: M ...

Rely on the razor method for generating URLs

I need to direct to a specific page, so I have implemented a JavaScript function in my MVC project: function rootUrl(url) { var _rootUrl = '@Url.Content("~")'; var x = url; if (url. ...

Tips for attaching to a library function (such as Golden Layout) and invoking extra functionalities

I am currently utilizing a library named Golden Layout that includes a function called destroy, which closes all application windows on window close or refresh. My requirement is to enhance the functionality of the destroy function by also removing all lo ...

Expanding size on hover without affecting the alignment of surrounding elements

Imagine there are 10 divs on the page styled as squares, collectively known as the home page. Among these 10: 3 divs belong to the .event1 class, 5 divs belong to the .event2 class, and 2 divs belong to the .event3 class. <div class="boxes event1"> ...

JavaScript text converter using split() method

I have a large amount of data in text format, similar to the following: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7e0b0d1b0c1b131f17124f3e19131f1712501d1113">[email protected]</a>:token1 | Time = US | Alphabe ...

The power of selenium meets the functionality of Javascript with the webdriver

Encountering an issue with Selenium JS Components are created in JSON format as follows: "usernameInputField": { "selector": { "xpath": "//*[@id='username']" } } For invoking webdriver, the following is used: var webdriver = ...

Instructions for linking a directive in Angular JS to a model containing keys that are generated dynamically

Imagine having a scope object defined like this: $scope.user = { pets : {} } and an element structured as follows: <my-pets id="pets" my-model="user.pets" data-title="Pet name" data-types="dog, cat, bird, hamster, exotic"> & ...

react component not displaying image

I must admit, this might be a silly question but I'm just starting out in web development. My goal is to create a clickable image component that redirects to another page on the website. However, despite not encountering any errors, the image is not ...

The "src" attribute is missing from the image on the left side

I'm currently facing an issue with the src attribute in this code: An event updates the src based on a variable retrieved from a form. The image files cannot be renamed, so I must work with their existing names. The problem arises as all file names c ...

Retrieve a specific nested key using its name

I am working with the following structure: const config = { modules: [ { debug: true }, { test: false } ] } My goal is to create a function that can provide the status of a specific module. For example: getStatus("debug") While I can access the array ...

Updating or swapping images using JavaScript and HTML

I am looking to implement a feature similar to Twitter, where uploading a picture automatically updates the avatar by displaying a spinner while the new image loads. I attempted to accomplish this with the following code: <script language="javascript"& ...

Having trouble inputting text into the text area using Selenium WebDriver with C#

Here is the original code snippet: I am having trouble entering text in the text box below. Can you please help me figure out how to enter text in this text box? <td id="ctl00_cRight_ucSMS_redSMSBodyCenter" class="reContentCell" ...

Utilizing two DTOs for a single controller in NestJS

I'm having trouble retrieving and transforming different types of dtos from the body. My goal is to extract and transform firstDto if it's incoming, or convert secondDto if that's what's being received. However, my current code isn&apos ...

Store the injected HTML within a PRE tag as a variable

My question pertains to a DIV HTML element that is responsible for displaying some HTML content from a variable: <div contenteditable="true" ng-bind-html="renderHtml(currentOperation.description)" ng-model='currentOperation.description&a ...