Is it only possible for ng-repeat to work with the variable {{ x }} when outside of angle brackets (<>)?

Just starting to explore the world of angularJS. Running into a little hiccup while experimenting with ng-repeat. Here's the code snippet causing me trouble:

<ul class="dropdown-menu" role="menu" ng-init="names=findDomains()">
<li><a style="cursor:pointer" ng-repeat="x in names" ng-click="selectDomain({{ x }})">{{ x }}</a></li>
</ul>

The concept is simple - I have a drop-down menu on my web page, let's call it "test". When clicked, it shows various options listed inside <li></li> tags. All these options are fetched by the function findDomains() and initialized using ng-init.

Upon selecting a specific option (for example, "opt1"), the text of the drop-down should be updated to display the selected option ("opt1" instead of "test"). The magic happens through the use of the selectDomain() function.

To achieve this functionality, I tried calling {{x}} twice within the ng-repeat loop thinking that by clicking on an option, the respective selectDomain() would get triggered.

However, despite everything else working perfectly fine (findDomains(), ng-repeat, and the second {{x}} outside the <a></a> tags), the {{x}} inside the <a></a> doesn't seem to function as expected. Clicking on options doesn't update the drop-down menu text.

Interestingly, the selectDomain() function works flawlessly when called with plain text (e.g., ng-click="selectDomain('opt1')).

Any suggestions or advice on how to address this issue?

Answer №1

Doctor's Note

ng-click requires an expression to be evaluated upon clicking

There is no need for interpolation with ng-click. It should not utilize {{}} interpolation directive, as it has direct access to scope variables within it.

ng-click="selectDomain(x)"

Answer №2

When you pass a variable to a function within a specific scope, you do not have to enclose it in {{}}.

Check out the following code snippet:

<ul class="dropdown-menu" role="menu" ng-init="names=findDomains()">
  <li><a style="cursor:pointer" ng-repeat="x in names" ng-click="selectDomain(x)">{{ x }}</a></li>
</ul>

Only use {{x}} when you need to display the variable value in HTML.

Answer №3

This task was actually quite simple. I just needed to delete the curly braces and insert ng-click="selectDomain(x)"

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

Assign the output of an XQuery query to a variable in JavaScript

Hi, I'm facing an issue that involves using XQuery on XML data stored in a variable. Here is an example of the XML structure: <channel> <available>yes</available> <label>CNN</label> </channel> <channel> <a ...

Generate a dynamic JSON object using JavaScript and then deliver it back

I'm dealing with a function that is supposed to return a JSON object in this format: this.sampleFunction = (x, filename) => { if (x.isPresent()) { return { 'result': true }; } else { return { 'result&apos ...

Ways to resolve - Error: Unable to access 'comments' property of null

I am currently working on developing a blog web application and I am facing an issue with enabling user comments on posts. Despite extensive research and attempts to troubleshoot by searching online, I have not been able to find a solution. I have been str ...

Adding a class to an element can be easily achieved when both input fields have values

Is it possible to apply the "active" class to the element go_back_right only when both inputs with classes search_box_name and search_box_id have content in them? I've tried looking for solutions on this platform, but being new to JavaScript, I couldn ...

Unable to establish a connection to the rootscope within an Angular 1.5 component

I am currently in the process of updating a legacy Angular 1.5 app by refactoring the architecture to eliminate what is commonly referred to as "scope soup". I found a helpful guide on how to do this here: My goal is to remove the reference to $rootscope. ...

Python Selenium driver.execute_script() is not returning expected values even though a return value is provided in the JavaScript script that is passed in

I am currently facing an issue with a selenium webdriver object while using the execute_script method. Despite inputting this JavaScript script: var data = document.getElementsByClassName("assignment__row break-word clickable flex--space-between ng-st ...

Is there a way to adjust a simple program using Discord.js where the setTimeout within a for-loop can print values sequentially instead of simultaneously?

Here is the code I'm using: for (let i = 0; i <= 5; i++) { delay(i); } function delay(i) { setTimeout(() => console.log(`${i} is the number`), 2000); } The output I'm currently getting after 2 seconds is: 0 is the number 1 is the ...

Refresh the angular-translate-static-loader-files within the app in Ionic without needing to reload the entire page

In the process of developing my Ionic app, I have incorporated angular-translate-loader-static-files along with angular-translate to import multiple language .json files. All aspects of this setup are functioning as expected; however, I am currently facing ...

Object FlipX using FabricJS

I'm struggling with flipping or mirroring an object horizontally on the FabricJS canvas when the object itself is clicked. I managed to get it close, but it was also flipping when resizing, which wasn't my intention. My initial thought is to add ...

`methods that exhibit peculiar behaviors`

My routes are set up with the get method. app.get("/info/teachers", controller1); app.get("/info/teachers/:teacherid", controller2); app.get("/info/students", controller3); app.get("/info/students/:studentid", contr ...

Guide on implementing a date selector for each button/option clicked using Vue.js

My experience with Vuejs is still fresh, and I've incorporated 3 buttons named chart1, chart2, and chart3. Whenever any of these buttons are clicked, I want a Date selection to appear in a radio type format. You can see an example below: https://i.ss ...

Error occurs when trying to map an array within an asynchronous function

Hey there, I have an array of objects with validation inside my async function (router.post()) and I need to map it before validating. Here is the approach I am taking: ingredients.map(({ingredient, quantity})=>{ if(ingredient.trim().length < 1 | ...

Utilizing Nested Click Events in jQuery to Enhance User Interaction

I'm really struggling with this and can't seem to find a solution anywhere. I want to capture data when button A is clicked, and then submit that data via AJAX when button B is clicked. Here's what I've been considering: $(document).o ...

Calculating the amount of results displayed through the ng-repeat filter

I am interested in using ng-repeat with filter, and I want to know how I can track the number of elements that are being filtered out. My goal is to implement a search bar that applies the filter, while also displaying the count of hidden elements (for e ...

Whenever I navigate to a new page in my NEXTJS project, it loads an excessive number of modules

I am currently working on a small Next.js project and facing an issue where the initial load time is excessively long. Whenever I click on a link to navigate to a page like home/product/[slug], it takes around 12 seconds to load due to compiling over 2000 ...

Using Node JS as both an HTTP server and a TCP socket client simultaneously

Currently, I am developing a Node.js application to act as an HTTP server communicating with a TCP socket server. The code snippet for this setup is displayed below: var http = require('http'); var net = require('net'); var url = requi ...

Node/Express: The $ symbol is failing to recognize the input for PORT 8080

What steps should I follow to run my PORT on 8080? Is it necessary to install any dependencies? const app = require('express')(); const PORT = 8080; app.listen( PORT, () => console.log('It's now running on http://localhost:$ ...

Encountered an issue with reading null properties when trying to access 'defaultPrevented' in bootstrap 5 toast

Implementing a custom hook to display and hide toast messages is working perfectly fine when invoking showToast in the button component of the Toast, but encountering an error when using this hook outside, for example in the button component of the App. Ty ...

Tips for utilizing a shared controller in an Angular Material popup dialogue

Within my project, Angular Material is being used for various dialogs, primarily for alert purposes. However, I now find myself in need of a more complex dialog. The example provided on the Angular Material site showcases how to create a dialog: function ...

"Utilizing Grunt to establish a connection with the base HTML tag

I have taken on the task of developing an angularjs website for a client, and the intended domain for hosting the site is similar to this: http://domain.com/newsite In order to ensure that my links function correctly in production, I included the followi ...