Mohamed Temraz
Mohamed Temraz

Reputation: 55

Angular passing parameter to a function

when i am passing the a parameter to the getActiveId function in the ng-click

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

it's value become zero in the controller and it's passed in the right value after testing it by the inspect element

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

Upvotes: 1

Views: 12807

Answers (2)

You need to drop the curly braces surrounding the parameter.

Here's how you should pass parameters to a function or, in any case, how you invoke functions or call scope objects inside directive's attributes:

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

UPDATE

To further elaborate why you must drop the double curly braces... these are used when you want to treat a statement as an expression. When it comes to directive's attributes, these are already expressions, so you must not use double curly braces. Makes sense?

Sometimes you'll find single curly braces inside directive's attributes, and this is used when you want to pass an inline javascript object as a parameter. You will see this often with ng-style directive:

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

Upvotes: 6

Milad
Milad

Reputation: 28592

ng-click and all angularjs directives that are native (ng-click - ng-model - ng-option , ...) all of them will accept expressions without two curleybraces .

So you must eliminate this : {{}} from inside of your ng-click like so :

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

But Notice that if you write a new directive of your own , and if you want to pass in any expression into it , you must use curleyBraces like this :

    in your controller: 

    $scope.color="blue";

    in your html :

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

   // This is a directive that will turn this span color , blue 

Upvotes: 2

Related Questions