Loop through each current value in an ng-repeat directive and use it in an

I'm facing a simple issue that doesn't seem to have an easy fix. I have a ng-repeat set up like this

<p ng-repeat="title in Menu.data[Menu.selected]">
    {{ title }}         
</p>

Now, I want to add an onclick event so I adjusted it as follows

<p ng-repeat="title in Menu.data[Menu.selected]" onclick="show({{ title }})">
    {{ title }}         
</p>

Unfortunately, the onclick="show({{ title }})" is not being interpreted by Angular. Any ideas why?


UPDATE: Just to clarify, show is a regular JavaScript function and not $scope.show

Answer №1

To simplify the code, avoid using double curly braces and instead just use

onclick="show(title)"

However, when working with angular, it is recommended to use the ng-click directive instead of onclick:

ng-click="show(title)"

In both cases, there is no need for curly braces.

The function show should be defined on $scope in your controller. Here's an example:

$scope.show = show;

Answer №2

To handle click events in Angular, it's recommended to use ng-click instead of traditional JavaScript event handlers. Since ng-click is an Angular directive, you don't need to wrap the function call in curly braces:

ng-click="displayInfo(title)" 

If the displayInfo function is defined globally, you can easily make it accessible on the scope like this:

$scope.displayInfo = displayInfo;

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

Tips for displaying a modal within a modal in React

Currently, I have two components with modals. My goal is to open one modal from a button in the other component but clicking the button only closes the current active modal and doesn't open a new one. //Modal 1 <div className="modal fade" ...

Having trouble displaying a JSON response on an HTML page with AJAX

I've written an AJAX function that goes like this: function ajx(){ var ajaxRequest; // This variable makes Ajax possible! try{ // Works in Opera 8.0+, Firefox, Safari ajaxRequest = new XMLHttpRequest(); } catch (e){ // For Internet Exp ...

There are two identical instances of the function and class named "Ajax" within the prototype

Within the current project I am involved in, we have incorporated and utilized a function called Ajax repeatedly throughout various sections. function Ajax (recvType, waitId) I am considering implementing the "prototype framework" for the Ajax class. Sh ...

Exploring the bind() method in the latest version of jQuery,

After upgrading my JQuery version, one of the plugins I was using stopped working. Despite trying to use the migrate plugin and changing all instances of bind() to on(), I still couldn't get it to work properly. The plugin in question is jQuery Paral ...

The issue arising where the callback function fails to execute after making an AJAX POST request

Within my function, I have an AJAX call that I want to make reusable. When the AJAX call is successful, I need to callback a function. var ajaxPostCall = function(data , url ,callback){ // Return the $.ajax promise $.ajax({ data: data, dataType: ...

JavaScript 'this' pointing to incorrect object

Unfortunately, this doesn't seem to be pointing to the correct object in this scenario. I'm having trouble figuring out how to reference the right one. function myObject() { this.someMethod1 = function() { var elementBtn = document.getEl ...

The validation feature is ineffective when used with Material UI's text input component

I need a function that can validate input to make sure it is a number between 1 and 24. It should allow empty values, but not characters or special symbols. function handleNumberInput(e) { const re = /^[0-9\b]+$/; const isNumber = re.test(e.target.val ...

Determine the fill color attribute value of a rectangle using Testcafe paired with typescript

Here is a code snippet I need help with: <colored-item label="Label A" symbol-size-left="9.5" symbol-size-right="12" symbol-right="" symbol-left="<svg viewport="0 0 24 24" xmlns="http://www. ...

The jQuery AJAX function successfully executes, but the MySQL post deletion operation fails to take place

I am encountering an issue with this particular code. The Ajax code runs through to the end and then fades out the parent of the delete button. Below is the code for the delete button, post, and Ajax: <?php include('php/connect.php'); ...

Is it possible to create an HTML link that prevents users from navigating back to the previous page or displaying the previous link in their browsing history?

I've been tasked with creating a button on a client's website that links to another page, like Google, for the purpose of assisting someone seeking help in case of an emergency. The challenge is finding a way to prevent the user from easily retur ...

Oops! Next.js Scripts encountered an error: Module '../../webpack-runtime.js' cannot be located

Looking to develop an RSS script with Next.js. To achieve this, I created a script in a subfolder within the root directory called scripts/ and named it build-rss.js next.config.js module.exports = { webpack: (config, options) => { config.m ...

Why is webpack attempting to package up my testing files?

In my project, I have two main directories: "src" and "specs". The webpack configuration entrypoint is set to a file within the src directory. Additionally, the context of the webpack config is also set to the src directory. There is a postinstall hook in ...

Arranging JSON data based on related properties in JavaScript

I have a JSON order with multiple products associated by vendorId, and I'm seeking to group them into separate arrays rather than having all of them listed together. How can I accomplish this? Here is my code: let order = { "product ...

Orbit around a moving object in Three.js

I am working with a camera that needs to rotate around a specific target position within the scene. Despite attempts to implement a pivot concept, as suggested in this discussion: https://github.com/mrdoob/three.js/issues/1830, I have come up with my own s ...

Prevent side-to-side scrolling on elements that exceed the width of the screen

I am working on building a navigation system similar to Google Play, where there are fixed tabs for browsing through the app. I have created a container div that holds various content sections. <div id="container"> <div class="section"> .. ...

Cancel promise chain after action dispatch (rxjs)

My goal is to achieve the following: this.jobManager .queue( // initiate a job ) .then( // perform additional actions, but stop if `ABORT` action is dispatched before completion ) .finally( // carry out necessary ...

Is RaphaelJS truly a vector-based tool?

Is it possible to position a circle at 50% of the width of the canvas using RaphaelJS without having to calculate the exact pixel value? I am looking for a simple way to place an element at half its container's width, but I'm not sure if this can ...

Testing Angular Factory Injection functionality

I have come across similar issues but haven't found a solution that works for my situation. The error message Error: [ng:areq] Argument 'fn' is not a function, got undefined is causing my test to fail. How can I properly inject a factory int ...

Adding x days to a Unix timestamp can be achieved by converting the Unix timestamp

Currently, I have the following code snippet: let currentTS = Math.floor(+new Date() / 1000) This code successfully retrieves the current date in a unix timestamp. However, my goal now is to append an additional 14 days to this unix timestamp. Could some ...

CreateAsyncModule using an import statement from a variable

When trying to load a component using defineAsyncComponent, the component name that should be rendered is retrieved from the backend. I have created a function specifically for loading the component. const defineAsyncComponentByName = (componentName: strin ...