How can I pass a dynamic scope variable to a JavaScript function in AngularJS that is being updated within an ng-repeat loop?

In my HTML, I have an ng-repeat loop where a variable is displayed in table rows. I want the user to be able to click on a value and pass it to a JavaScript function for further action. The code snippet below showcases my earlier version which successfully displays host values from filteredList with link attributes:

<tr data-ng-repeat="update in filteredList">
  <td> <a href="#">{{update.host}}</a> </td>
  <td> {{update.date}} </td>
  <td> {{update.num}} </td>
</tr>

However, my attempt to pass the clicked host value to my "searcher" function using ng-click doesn't work as expected:

<tr data-ng-repeat="update in filteredList">
  <td> <a href="#" ng-click="searcher({{update.host}})">{{update.host}}</a> </td>
  <td> {{update.date}} </td>
  <td> {{update.num}} </td>
</tr>

AngularJS throws an error when encountering this code: Error: [$parse.syntax] Syntax Error: Token 'update.host' unexpected, expecting [:] at column 12 of the expression [searcher({{update.host}});] starting at [update.host}});].

I am seeking advice on how to properly pass the clicked host value to my function in a way that AngularJS accepts.

Thank you!

Answer №1

Multiple individuals have pointed out in the comments that the following code will be effective for your situation:

<a href="#" ng-click="searcher(update.host)">{{update.host}}</a> </td>

For more details, you may want to refer to the documentation related to ng-click. It explains that ng-click accepts an expression (thus eliminating the need for using {{binding}} syntax).

Answer №2

This is the correct method to accomplish this task

<tr data-ng-repeat="result in filteredData">
  <td> <a href="#" ng-click="search(result.keyword)">{{result.keyword}}</a> </td>
  <td> {{result.time}} </td>
  <td> {{result.count}} </td>
</tr>

Answer №3

Give this a shot:

<td> <a href="#" ng-click="searcher(update.host)">{{update.host}}</a> </td>

Just a reminder, there's no need to include the curly brackets around variables within ng-click or any other angular directive parameter.

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

The v-tab-item content is not loading properly when the component is initialized in the mounted hook

I'm working on a project that utilizes Vuetify tabs to showcase two different components under separate tabs. The problem I'm encountering is that, within the mounted() function, when attempting to access the refs of the components, only the ref ...

Is there a more "Angular-esque" approach to implementing this (inter-element communication)?

I have created a custom directive that will automatically add an asterisk to the label of any input field marked as required. The following is my link function with detailed comments: // This is what the DOM structure looks like: // <label id="label-1" ...

The verification of form is not done using an if statement

There are two forms in my code named formA and comments that I need to submit via AJAX. However, the current if and else conditions do not correctly identify the form and always trigger the alert message hello3. Here is the JavaScript function: function ...

How can I design a versatile button in HTML/CSS3/jQuery/Javascript that utilizes images for borders and corners for maximum scalability?

Is there a way to create a completely scalable button in HTML/CSS3/jQuery+Plugins using images for borders and corners? I have attempted a messy method, but I am confident that there are more effective solutions available. The approach I considered invol ...

Customizing Magnific Popup: Changing showCloseBtn and closeOnBgClick settings during display

Is there a way to customize the behavior of an open instance of a magnific popup? I want to have different settings for when the popup is closable and when it should be prevented from closing. It appears that these options are only available during initial ...

There are various IDs in the output and I only require one specific ID

I have a JSON fetcher that is functioning properly. However, whenever I request an ID, it returns all the IDs present in the JSON data. Is there a way to retrieve only the latest ID? This is my first time working with JSON so I am still learning. $(docu ...

The cookies() function in NextJS triggers a page refresh, while trpc consistently fetches the entire route

Is it expected for a cookies().set() function call to trigger a full page refresh in the new Next 14 version? I have a chart component that fetches new data at every interval change, which was working fine when fetching the data server-side. However, since ...

The ElementNotVisibleException error was triggered because the element is currently hidden from view, making it unable to be interacted with through the command

I'm facing an issue when trying to clear the text box. The error message I am receiving is: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 30.14 se ...

Turn off the highlighting for an external event in FullCalendar

Hey there, I'm currently working with the fullcalendar jquery plugin v2.6.1 and I have a question about preventing external events from being highlighted while they are being dragged onto the calendar. Is there a way to stop the fc-highlight from app ...

Is there a way to handle templates in AngularJS that is reminiscent of Handlebars?

Is there a way to handle an AngularJS template using a syntax similar to Handlebar? <script type="text/ng-template" id="mytemplate"> Name is {{name}} </script> I know how to retrieve the template using $templateCache.get('mytemplate&ap ...

The function insertAdjacentHTML does not seem to be functioning properly when used with a

I am working with a parent element that contains some child elements. I create a DocumentFragment and clone certain nodes, adding them to the fragment. Then, I attempt to insert the fragment into the DOM using the insertAdjacentHTML method. Here is an ex ...

What is the best way to use jQuery to find and select an "a" tag that links to a file with a specific

My goal is to select links that have different types of files using jQuery: jQuery('a[href$=".pdf"], a[href$=".doc"], a[href$=".docx"], a[href$=".ppt"], a[href$=".pptx"], a[href$=".xls"], a[href$=".slxs"], a[href$=".epub"], a[href$=".odp"], a[href$=" ...

AngularJS Error Present in Chrome Only

While this code works perfectly in Firefox and IE, it encounters an issue in Chrome. An error occurs at the ".find" line which results in a "TypeError: undefined is not a function". angular.forEach(data, function(data) { pointInfoFactory.ge ...

What is the best way to execute a JavaScript file with npm scripts?

Trying to use npm but encountering some issues. In my package.json file, I have: "scripts": { "build": "build.js" } There is a build.js file in the same folder that simply console.logs. However, when I execute npm run build I receive the error messag ...

passport.authenticate method fails due to empty username and password values

I seem to be making a simple mistake while following a tutorial. Even though I believe I have followed all the steps correctly, when I submit the login form, I get redirected to the "failureRedirect" page. When I checked the source code in the passport mod ...

The potential issue of undefined objects in TypeScript when utilizing computed properties in Vue3

I've attempted adding a ? after each word and also experimented with the following code: const totalNameLenght = computed(() => { if (userFirstnameLenght.value && userLastnameLenght.value){ return userFirstnameLenght.value + u ...

Nuxt.js implemented with Auth using jwt refresh tokens

I've implemented the Auth library in my Vue/Nuxt project and have successfully set up JWT Authentication. However, I'm encountering an issue with the refresh token. The refreshToken cookie is consistently being set to null: Furthermore, when at ...

Steps for uploading an Excel file in an MVC controller

Currently, I am attempting to upload an Excel file using AngularJS + MVC. The view consists of an HTML file with a basic HTML file upload functionality. Once the file has been uploaded, my goal is to be able to access the Excel file within the MVC controll ...

Trigger a function when clicking outside the element, similar to how a Bootstrap modal is closed

I have successfully created a popup box using angular in my project. It appears when I click on an icon and disappears when I click on the close button. However, I would like it to also close if someone clicks outside of the popup. Can anyone help me with ...

One project contains a pair of React instances

I am currently working on a React Web App and recently encountered an issue with an 'invalid hook call' error. Upon further investigation, I discovered that there are duplicate copies of the React library in my project, including within the CSS f ...