AngularJS: Challenges with Binding in event handlers within ng-repeat loops

I'm currently working on a project where I need to display buttons for choosing a username using ng-repeat. The display of the buttons is working fine, however, I have encountered an issue with the ng-click directive within the loop. Even though the binding appears correct in Google Chrome's DOM Explorer, when one of the buttons is clicked, the event handling function login() receives {{user.userId}} as input instead of the actual value of the parameter.

Here is the code snippet:

<div class="cell large-3" ng-repeat="user in userData()" ng-class="{'large-offset-1': ($index % 3) != 0}">
      <button ng-click="login('{{user.ID}}')" class="button expanded red bigButton"> {{user.userName}} </button>
    </div>

Controller Code:

(function () {
'use strict';
angular.module('app').controller('loginCtrl', ['$scope', '$controller', '$location','dataService', 'mainService', initializeLoginCtrl]);
function initializeLoginCtrl($scope, $controller,$location, dataService, mainService) {
  angular.extend(this, $controller('baseCntrl', {
            $scope: $scope
        }));

  $scope.userData = dataService.getUserData;

  $scope.login = function(userID) { <-- input of userid is {{userData.ID}} instead of the eal userID from Binding
    mainService.login(userID);
  }
}

})();

Any help or guidance would be greatly appreciated! Thank you!

Answer №1

ng-click="login('{{userData.ID}}')"
-> ng-click="login(userData.ID)"

Make sure to pass the userData.ID as a parameter without using quotes.

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

We implemented a unique constraint in the email field of the MongoDB index to prevent duplicate registrations and notify users who are already registered

Enforcing the unique:true constraint prevents data from being added to the database, only triggering the catch(e) error message. I attempted to use the findOne method, but due to my lack of experience, the architecture continues to confuse me. require(&apo ...

Adjust the Material UI card to fill the maximum available height

I'm currently working with Material UI Card components. You can find more information here. My goal is to set a maximum height for these cards, ensuring that the text and images are displayed nicely. How should I approach this? Below is a snippet of ...

Getting Started with Angular and Express

I've been able to easily set up Angular in my web app using ASP/Visual Studio, but I'm interested in delving into the world of Node, specifically Express. However, I'm struggling to grasp the concept of a basic route handler for Express that ...

Is it truly necessary to remove packages from devDependencies to improve performance?

It is a common understanding that packages listed under devDependencies are typically not included in the final build. So why do we remove them for the sake of performance optimization? For instance, there are discussions about replacing Moment.js with a ...

Next.js application experiencing unexpected behavior with React state update and useEffect

Encountering unexpected behavior with state updates and useEffect in my Next.js app. The lock/unlock feature in a component is not functioning as intended. Below is the relevant code snippet: const [isLocked, setIsLocked] = useState(false); useEffect(() = ...

Adjust the dimensions of the canvas without disrupting the existing artwork

I am currently working on a pixel art application where I am facing an issue with saving images from the canvas. The saved image appears small and pixelated when I try to resize it. I would like to resize the image to larger dimensions, but I am unsure of ...

Sort the activity based on $routeParams

I am structuring my routes in a way that is similar to Rails. Here are some example routes I have set up: $routeProvider.when('/articles', { controller: 'ArticlesCtrl', templateUrl: '/views/articles.html' }); $routeProvid ...

JavaScript if statement to check for either one, but not both

Hey there, fellow developers. I'm currently encountering a mental block and struggling to find a solution for my issue. Here is the code snippet in question: if ((n % 3 === 0 || n % 5 === 0) &&( n % 3 !== 0 && n % 5 !== 0)) { ...

Having trouble getting the focus-within CSS pseudo-class to work with an HTML label

I'm attempting to create a dropdown menu with checkboxes inside the dropdown area. Below is the code snippet I am using: .search-box { color: black; width: 450px; } .search-box .fake-tb { display: flex; flex-wrap: nowrap; background: #f ...

What is the most effective method for iterating through a JavaScript array?

Let's discuss an interesting way to work with JavaScript arrays. Consider the array below: const allRows = [ {id: 1, name: Name 1}, {id: 2, name: Name 1}, {id: 3, name: Name 1}, {id: 4, name: Name 1}, {id: 5, n ...

Attempting to organize date and time down to the second

I'm currently working on sorting time with seconds included. While I am able to sort the minutes successfully, I'm facing difficulty in sorting the seconds. Despite trying various approaches and using dynamic data that needs to be sorted in desce ...

I'm looking for some clarity on incorporating <SpeedInsights /> into NextJs14. Can anyone shed some light on

While working on my NextJs project, I attempted to integrate Vercel Speed Insight import { SpeedInsights } from "@vercel/speed-insights/next" <SpeedInsights /> An error related to hydration is being returned. I am looking for an explana ...

What could be the reason for receiving no file input after submitting the form?

One of the functions triggers the input file and displays previews: $(document).on("change", "#Multifileupload", function() { var MultifileUpload = document.getElementById("Multifileupload"); if (typeof FileReader != & ...

Securing your app with Node.js and Passport authentication

After setting up my first node.js app, I ran into some issues with configuring passport for login functionality. To simplify my app.js file, I created a /app/config/express.js file for configuring express components: var app = require('./app/config/ ...

Ways to interact with similar dynamic controls in Javascript

I have an aspx page with a Select box control: <select name="selViewPerPage" id="selViewPerPage" style="width:30px"> To ensure consistent styling across all browsers, I am replacing this html control with a dynamic select box using "selectBox.js". ...

Iterate through the collection to retrieve the value associated with a specific key

In the JSON response I have received, I am trying to extract the value of "fees" corresponding to "detailComponent" with the identifier "FULL_FEE". However, instead of retrieving the desired value, the loop seems to be fetching the last value associated wi ...

What is the best way to stop the browser from automatically redirecting to another page after submitting a form?

I am using an AJAX call to a method that returns JSON data. How can I retrieve and read the JSON response without being redirected to a new empty page? [HttpPost] public JsonResult Test() { return Json("JSON return test", JsonRequestBehavior.AllowGe ...

Modify the anchor text when a malfunction occurs upon clicking

Whenever I click on the link, I am able to retrieve the correct id, but the status changes for all posts instead of just the selected item. Below is the HTML code: <div th:each="p : ${posts}"> <div id="para"> <a style="float: right;" href= ...

Is it possible to use jQuery's .attr method to change the href attribute if a certain text is contained within a DIV

(Twitter) I'm struggling with how to incorporate the .attr method at the start of links/A/href when the selected element contains specific words: $(".tweet:contains('government','Anonymous')").each(function(){ $old_url = $(thi ...

Using JQuery, Ajax, and PHP for a secure login system

I need a handler for my login process to verify the username and password in the database and redirect the user to another page if the credentials are correct. I am attempting to achieve this using JQuery Ajax and PHP. I have created a JS script to send t ...