Using Angular to pass parameters to a function

When passing a parameter to the getActiveId function in the ng-click

<span class="col ion-ios7-heart-outline center" ng- click="getActiveId({{activeSlide.selected}})">

The value turns to zero in the controller, but it is passed correctly after testing using inspect element

<span class="col ion-ios7-heart-outline center ng-binding" ng-click="getActiveId(1)"> 1:</span>

Answer №1

Make sure to remove the curly brackets around the parameter.

When passing parameters to a function or calling scope objects within a directive's attributes, follow this example:

<span class="col ion-ios7-heart-outline center" ng-click="getActiveId(activeSlide.selected)">

UPDATE

It's important to note why using double curly braces is not necessary in this context... Double curly braces are meant for treating statements as expressions. In directives' attributes, expressions are already expected, so double curly braces are redundant. Clear?

Sometimes you may see single curly braces inside directive's attributes, indicating the use of an inline JavaScript object as a parameter. This is commonly seen with the ng-style directive:

<div ng-style="{color:red}">

Answer №2

When using ng-click and other native AngularJS directives (such as ng-model and ng-option), you can provide expressions without the need for double curly braces.

Instead of including {{}} inside your ng-click directive, simply write:

<span ng-click="getActiveId(activeSlide.selected)">

However, it's important to note that if you create your own custom directive and want to pass an expression into it, you must use curly braces like so:

In your controller:

$scope.color = "blue";

In your HTML:

<span turnToThisColor="{{color}}"></span>

// This directive will change the color of the span to blue

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

Utilizing Fancybox with a polymer paper-card: A step-by-step guide

I have a collection of paper-cards and I am looking for guidance on integrating the fancybox library to display the content of each paper-card. Here is a sample of a paper-card: <paper-card heading="Emmental" image="http://placehold.it/350x150/FF ...

Place the Div directly above a distinct Div

Hey there! I'm currently working on positioning one div on top of another div programmatically (using javascript or css). These divs are completely separate and have the following structure: <div class="bottom"> <img src="image"></i ...

Unable to display the column data labels in Highcharts due to the incorrect formatting being presented

I'm having trouble displaying the datetime format at the top of a column in my chart. Although the tooltip shows the correct data when hovering over the columns, the labels are not formatted properly. Received output - 86340000 Expected output - 23: ...

Customize the element of the root node of a MUI component using the styled()

I am trying to implement the "component" prop with a MUI component (such as ListItem) using the styled() API. However, I am facing an issue where it says that "component" is not a valid prop. Can someone guide me on how to correctly achieve this? I have se ...

What is the reason for IE displaying null when the model does not exist?

Why does IE 11 render 'null' if my model does not exist? For instance: <tr> <td [innerHTML]="model?.prop1 | my-pipe"></td> </tr> Imagine this scenario: When the page loads, a request is sent to the server and the res ...

Displaying hidden divs upon mouse hover using

Currently, I am in the process of creating a website that features a header with two rows of names. The goal is to have a specific div appear below the names when a user hovers over them. This div will include additional images and links for users to inter ...

tips for successfully transferring date and time data between json and nosql databases like firestore

Input: Created_At:Monday, 29 April 2019 15:07:59 GMT+05:30 Updated_At:Monday, 29 April 2019 15:07:59 GMT+05:30 I attempted to export data in JSON format from Firestore using the npm package firestore-export-import. However, the output I received was: ...

Are the site-wide navigation links working on one page but not the other?

Currently, I am in the process of constructing a website on rails and have successfully integrated a site-wide navigation bar to display on all pages. However, an issue has arisen where the navbar is visible on both pages, yet the links and hover effects a ...

Enhance the angular 2 dependencies within the angular2-cli project

After experimenting with Angular 2 and following the guide on their website, I attempted to switch to Angular 2 CLI. However, the Angular 2 CLI project does not have the latest dependencies, resulting in errors from the compiler related to certain commands ...

How can I stop TypeScript from causing my builds to fail in Next.js?

Encountering numerous type errors when executing yarn next build, such as: Type error: Property 'href' does not exist on type '{ name: string; }'. This issue leads to the failure of my build process. Is there a specific command I can ...

Tips for postponing the execution of following tasks until the completion of the setState and ensuring that they are reliant on

I'm encountering an issue with the useEffect hook in my React app that uses useState. Here's the code snippet: const [jobTypes, setJobTypes] = useState([]); const getJobTypes = async () => { try { const response = await fetch(&a ...

Using Node.js to schedule and send notifications to a specific topic via FCM

In order to deliver topic notifications to Android devices using FCM, I have set up an Angular application and a Node.js server. The scheduling of these notifications is handled by the bull library. The process involves two methods of sending notification ...

Using Javascript to dynamically retrieve accordion content from a dynamically generated ID

I am currently working on developing a more intricate accordion feature. The accordion will consist of 4 buttons (section 0-3) with unique content (lorem ipsum 0-3). Clicking on "section 0" should reveal "lorem ipsum 0", while clicking on "section 1" shoul ...

Reaching SVG with CSS

Is it possible to target SVG elements with CSS? In older versions of IE, they display like broken images and I want to hide them using modernizr. I'm looking for a solution like the following... .no-svg object[type=svg] { display:none; } I have be ...

Utilize AngularJS to enable Semantic UI component without the need for jQuery

Currently, I am in the process of developing a website using AngularJS and Semantic UI. While everything seems to be working well with the CSS components, I have encountered an issue when trying to activate the JavaScript components. For instance, I typic ...

When I try to add more content to my page, it doesn't extend beyond a single page and instead gets compacted together

As a beginner in the world of coding, my goal is to create a time management website specifically tailored for school use. Despite copying this code multiple times, I have encountered an issue where it does not continue down the page, and I am unsure of th ...

Retrieving a unique header from the API

I am attempting to send a personalized header to an API that is under my control using an AJAX request through AngularJS. Client (AngularJS) var auth_token = 'qwerty'; var custom_value = 'abc123'; $http.get('http://api.mywebsite ...

Contrasts between Flash and HTML5

Now that YouTube has transitioned from flash to HTML5, what are the unique features and functionalities of HTML5 that set it apart from flash? I am also curious to learn a more in-depth and intriguing aspect of HTML5 beyond what can be found through a sta ...

Is it possible to integrate HTML pages as sections within an HTML file using AngularJS?

There are three individuals each working on separate sections of a webpage. One is focusing on moving images, another is devoted to the tab section. This begs the question: 1) Is it feasible to embed all these HTML sections into a single HTML page and dis ...

How to Set Up Bower in WebStorm

I require assistance with the installation process of bower in WebStorm. My current version is 11.0.2 and I have a package.json file that needs to include bower.json for implementing a date-picker in Angular.js. To achieve this, I need to install bower. Th ...