When using ngClick with a parameter, the parameter is not being successfully passed

My table resembles a tree structure with two ng-repeats.

<table>
<tr ng-repeat-start="am in anArray">
<td><button ng-click="TheFunction(am)"></button></td>
</tr>
<tr ng-repeat-start="em in anotherArray">
<td><button ng-click="TheFunction2(em)"></button></td>
</tr>
<tr ng-repeat-end></tr>
<tr ng-repeat-end></tr>

By applying some functions to toggle the "correct" line, a tree-like structure is displayed on the screen where each row contains a button that triggers a function defined within the scope. In the controller, I have implemented the following functions:

$scope.TheFunction=function(am){
alert(am); //and other operations involving the am object
}
$scope.TheFunction2=function(em){
alert(em); //and other operations involving the em object
}

Despite the working functionality of the tree-like construct on the webpage and the proper execution of the respective functions upon button clicks, the passed parameters appear to be undefined when checked.

Could there be something that I overlooked?

Answer №1

Here is the code snippet that worked seamlessly with both ng-repeat and ng-repeat-start. I opted for ng-repeat as it provides cleaner results.

Feel free to test this out:

var app = angular.module('app', []);
app.controller('main', main)

function main($scope){
  $scope.anArray = ["a", "b", "c"];
  $scope.anotherArray = ["dog", "cat", "mouse"];
  $scope.TheFunction=function(am){
    alert(am); //perform actions on am object
  }
  $scope.TheFunction2=function(em){
   alert(em); //perform actions on em object
  }

}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div ng-app ng-view ng-controller="main">
<table>
<tr ng-repeat="am in anArray">
  <td><button ng-click="TheFunction(am)">{{am}}</button></td>
</tr>
<tr ng-repeat="em in anotherArray">
  <td><button ng-click="TheFunction2(em)">{{em}}</button></td>
</tr>
</table>
</div>

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

Text about the three.js orthographic camera: "Exploring the

I'm trying to implement an orthographic camera overlay for a heads-up display on top of my main camera. I'm having trouble getting any text to show up, here's the code snippet: function setupHUD() { var width = window.innerWidth; var h ...

in JavaScript arrays, the identifier follows the numeric value right away

Can anyone assist me with assigning a PHP array of image filenames to a JavaScript array? I have made some progress, here's my code: <script src="jq.js"></script> <?php $dir = 'images'; $images = scandir($dir); $true_images ...

Ensure your TypeScript class includes functionality to throw an error if the constructor parameter is passed as undefined

My class has multiple parameters, and a simplified version is displayed below: class data { ID: string; desp: string; constructor(con_ID:string,con_desp:string){ this.ID = con_ID; this.desp = con_desp; } } When I retrieve ...

How can I add a blank selection at the bottom of my ng-options dropdown?

At the moment, my setup looks something like this (in a simplified form): <select ng-model=model.policyHolder ng-options="person.index as person.name for person in model.insurance.persons"> <option value>Someone else </select> This co ...

The concept of setTimeout and how it affects binding in JavaScript

I have limited experience with jquery, mainly using it for toggling classes. However, I will do my best to explain my issue clearly. I have three div elements and when one is clicked, the other two should rotate 90 degrees and then collapse to a height of ...

What is the process for calculating the total sum of input values utilizing JavaScript?

My JavaScript skills are not perfect, and I'm struggling to calculate the total sum of values in the amount input boxes without refreshing the page. Can someone assist me with this challenge? Thank you. function Calculat ...

What is the best way to combine two objects in AngularJS?

https://i.sstatic.net/qr5Bb.png If you have two JSON objects that need to be merged while removing duplicate properties, what approach would you take? ...

How to iterate over the request body in Node.js using Express?

When I send a request with data in the form of an array of objects: [ {id: "1"}, {id: "2"}, {id: "3"} ] I am utilizing JSON.stringify() and my req.body ends up looking like this: { '{"id":"1"} ...

Struggling to get JavaScript to successfully load a .txt file in order to load various links

I created a cool feature that takes users to a random link, but I currently have a large list of links and wanted to find a way for JavaScript to read them from a text file. The code I have tried so far is not working, and I have experimented with other s ...

Executing JavaScript code within a class object using ASP-VB

I'm looking to create a function that will show a Javascript-based notification. I already have the code for the notification, but I'm trying to encapsulate it in a class library as a function. However, I am unsure about what to return from the f ...

Sorting and Deduplicating MongoDB Data with Reduce

I'm currently using the Reduce function to generate a combined String of fields from an array. For instance, suppose I have an array of subdocuments named children - and each individual child contains a name field. For example: [ {name:"Zak"}, {n ...

individualized django models field for each user

Is it possible to create a boolean field in the post model to track if a user has liked a post? This field should only be changed by the respective user and not affect others. For example, if user 1 likes a post, it should show as true only for that user ...

Creating "search result pages" with HTML, PHP, Javascript, and MySQL

Consider the scenario where you have a table with two columns: id and name, containing thousands of rows. You have a search engine that allows searching by free text, using the query: SELECT * FROM `table` WHERE `name` LIKE '%$search%' The res ...

Using Flask for AngularJS authentication

I am currently in the process of creating a web application utilizing AngularJS along with a REST-API developed using the Flask framework. This application is broken down into two main components: The first part allows users to interact without loggin ...

What are the benefits of integrating a jQuery plugin into an Angular directive?

We have integrated multiple jQuery plugins into our project, and now we are looking to transition it to AngularJS. What would be the best approach for incorporating this functionality in an Angular perspective? Should we create a directive for each plugin ...

Using base64 encoding and setting the binary type for Websockets

I'm feeling a bit lost. I've been working on understanding the mechanics of Websockets by setting up a server in node.js and a client-side application. One thing that's really puzzling me is how Websockets handle data transmission. Should da ...

Having trouble decoding invalid JSON received from the Twilio API

For the past 4 hours, I've been struggling to parse a basic JSON from Twilio. The process is as follows: A text message containing a magnet link is sent Twilio forwards the request to my serverless function in the cloud I attempt to parse the reques ...

Manipulate Nested Objects using an Array of Keys

Currently, I am developing a recursive form using React and MUI that is based on a nested object. Throughout this process, it is crucial for me to keep track of the previous keys as I traverse through the recursion. As users interact with the form and mak ...

I am trying to showcase a collection of images on my homepage, but unfortunately, it is not functioning as expected

Does anyone know how to display images using Angular? I am currently utilizing the pic controller in an AngularJS file. Any assistance would be greatly appreciated. HTML CODE <!DOCTYPE html> <html> <head> <meta charset="utf-8"& ...

Tips for accessing the value of a dynamically created textbox using JavaScript

Hello everyone, I have a couple of questions that I need help with. I am currently working on developing a social networking website similar to Facebook. On this platform, there are multiple posts fetched from a database. However, I am facing an issue w ...