Retrieve data from a ng-repeat variable within a controller

Below is the current code snippet:

HTML


            <center><li ng-repeat = "x in items | orderBy: 'priority'"> 
            <!-- color code priorities -->
            <span ng-style="cmplt" ng-class="{ red: x.type == 'Work', blue: x.type == 'Personal' }">
                <b>{{ x.name }}</b></span>
            <span ng-class="{ yourChore: x.assignedTo == username} ">
                - {{ x.type }} (Priority {{ x.priority }}) Assigned by {{ x.creator }} to {{ x.assignedTo }}
            </span>&nbsp;&nbsp;

 <!-- When task is completed, user can click this button to mark it as such -->
            <button type="button" ng-click="cmplt={color: 'gray', 'text-decoration': 'line-through'} ; 
                markAs(this)">Completed</button>
            <div ng-class="{ red: x.completed == true }"> Hello</div>
            <button type="button" ng-click = "comment = true">Comment</button>
            <div ng-show="comment"><textarea rows="3" columns="50" ng-model="x.comments"></textarea></div>
            <div>{{ x.comments }}</div>
        </li></center>

JavaScript


    $scope.markAs = function(repeatScope){
    if (!repeatScope.completed){
        repeatScope.completed = true;
    }
    else {
        repeatScope.completed = false;
    }
};

The objects being repeated have a boolean value inside them that defaults to false. However, when the user clicks a button, it should change to true. The issue is that this does not occur as expected, and the value remains false. It's unclear why this is happening based on the provided code.

Answer №1

By using the ng-repeat directive, you have the ability to access the current element like so:

<li ng-repeat="x in items"> <!-- reference to x within the repeat scope -->
    <button type="button" 
            ng-click="cmplt={color: 'gray', 'text-decoration': 'line-through'}; markAs(x)">
        Mark as Completed
    </button>
</li>

Answer №2

Here's a simple solution to the problem:

Consider implementing something along these lines:

ng-repeat = "x in items"

Each time an item is repeated, x will be used as reference.

markAs(x)

This will trigger the function with the correct x as input.

Answer №3

Make sure to pass x, the current item in your ng-repeat, instead of using this.

<button type="button" ng-click="cmplt={color: 'gray', 'text-decoration': 'line-through'} ; 
            markAs(x)">Mark as Completed</button>

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

Reduce the length of the text to 50 characters after the current word, while ensuring that the word

Looking for a way to shorten text after reaching 50 characters, making sure not to split words in the middle when cutting off. For example: Contrary to popular belief, Lorem Ipsum is not simply text (59 chars) Desired output: Contrary to popular belief, ...

What could be causing the render to not appear? Using Aframe with three object3D elements

I am having trouble rendering a threejs object in aframe. What steps should I take to successfully render the object? html <a-scene> <a-entity geometry material id="obje"></a-entity> <a-entity camera id="cam"&g ...

Struggling with the addition of the 'Other' option in React manually. Any tips or suggestions?

I've encountered a slight issue regarding the functionality of my React Component below. Specifically, I want users to have the ability to enter a poll category that is not listed in the select component options by using an input field. This scenario ...

What is the best way to add this dependency to an AngularJS application?

I am struggling to properly implement the injection of this dependency: https://cdnjs.cloudflare.com/ajax/libs/angularjs-dropdown-multiselect/2.0.0-beta.10/src/angularjs-dropdown-multiselect.js within my project. This is the current dependency injection s ...

Exploring ngModel components with a specified class

In my HTML code, I've assigned many elements with ngModel defined as ng-model = "object.[something]". For example: <div class="form-group row" ng-model="object.askUser"> I use this method to keep my purpose clear for these elements. My questi ...

Implement AJAX and PHP to submit form data along with the page ID and session information

I am currently developing a school book library website project that allows students to browse and select books, as well as send requests to the librarian to borrow them. However, I am still learning jQuery and facing some challenges in handling issues. H ...

Looking for assistance with resizing and repositioning an image within a viewport?

The JSFiddle code can be found at this link - https://jsfiddle.net/pmi2018/smewua0k/211/ Javascript $('#rotate').click(function(e) { updateImage(90, 0) console.log("rotation"); }); $('#zoom-in').click(function() { updateImage(0 ...

Show Pop in relation to modified Text

When the user clicks on the text, a pop-up is displayed after the last word in the text.... https://i.stack.imgur.com/VWQCa.png The logic being used is : left = layer.width + layer.x Code : document.getElementById(lightId).style.left = layer.x + docume ...

Stripping out only the position attribute using jQuery

I have implemented a code with the following characteristics: The navigation items' texts are hidden behind certain Divs, which I refer to as Navigation Divs. When the mouse hovers over these pixels (navigation Divs), the text that is behind the ...

The HTTP request arrives with no content within the body

I am in the process of developing a basic client-server application using Node and Express. The goal is for the program to receive a JSON input on the client-side, perform some operations, and then send data to the server-side. Currently, I am able to sen ...

Linking Redux to the highest level of my application is not functioning as expected

I have been attempting to troubleshoot this code for quite some time now, but I am struggling to identify the issue at hand. My main goal is to establish a connection between my top-level application and the redux store. However, every time I try, the stor ...

The JavaScript setTimeout function not triggering repetitively for 10 instances

I'm facing an issue with a JavaScript/jQuery function that is designed to call itself multiple times if there is no available data. This is determined by making a web service call. However, the logging inside the web service indicates that it is only ...

Using radio buttons to toggle the visibility of a div element within a WordPress website

I am currently working on creating a WordPress page using the custom page tool in the admin interface. My goal is to have 3 radio buttons, with 2 visible and 1 hidden. The hidden button should be automatically checked to display the correct div (although ...

Body section CSS selector

Can a CSS selector be included within the body section of an HTML document? Here's an example of my code (although it is not functioning as expected): <html> <head> </head> <body> <div style= "a[target=_blank] {backgroun ...

Linking several asynchronous functions together in JavaScript

class Calculation { constructor(num) { this.num = num; } performAddition() { // code } performSubtraction() { // code } performMultiplication() { // code } performDivision() { // code } } const getResult = async ...

Struggling to make Tumblr Javascript function properly

I have integrated the following code snippet: <script type="text/javascript" src="http://underlineent.tumblr.com/api/read/json"> </script> Unfortunately, my Tumblr blog is not appearing on this site. Can anyone provide assistance? The provid ...

Ways to display console.log output in VS Code?

Sorry if this question is a bit unclear, but I'm struggling to figure out how to display the results from my console.log (line 14) in the console/terminal. I want to see the random RGB values generated for each column every time I click the button, bu ...

Reading Properties in VueJS with Firebase

<template> <div id="app"> <h1 id="title"gt;{{ quiz.title }}</h1> <div id="ques" v-for="(question, index) in quiz.questions" :key="question.text"> <div v-show="index = ...

Utilizing repl.it for a database in Discord.js

I've created a database script on repl.it and it seems to be functioning properly. However, I keep encountering null values in the users' database. Here's my code snippet: client.on("message", async (message) => { if (messag ...

Exploring the potential of Oracle Openscript in combination with Javascript using AngularJS,

I have encountered an issue while using Openscript on a form page with a clickable "save" button implemented as a div. Manually clicking the button triggers a javascript event that saves any changes made on the page. However, when I run the script, the but ...