角度传递参数到函数

时间:2022-12-05 14:41:37

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

当我在ng-click中将a参数传递给getActiveId函数时

<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

它的值在控制器中变为零,并且在由inspect元素测试之后传递到正确的值

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

2 个解决方案

#1


5  

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

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:

有时您会在指令的属性中找到单个花括号,并且当您想要将内联javascript对象作为参数传递时使用。你会经常看到ng-style指令:

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

#2


1  

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

ng-click和所有本机的angularjs指令(ng-click-ng-model-ng-option,...)所有这些指令都将接受没有两个curleybraces的表达式。

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

所以你必须从你的ng-click里面消除这个:{{}},如下所示:

     <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 :

但请注意,如果您编写自己的新指令,并且想要将任何表达式传入其中,则必须使用如下的curleyBraces:

    in your controller: 

    $scope.color="blue";

    in your html :

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

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

#1


5  

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

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:

有时您会在指令的属性中找到单个花括号,并且当您想要将内联javascript对象作为参数传递时使用。你会经常看到ng-style指令:

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

#2


1  

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

ng-click和所有本机的angularjs指令(ng-click-ng-model-ng-option,...)所有这些指令都将接受没有两个curleybraces的表达式。

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

所以你必须从你的ng-click里面消除这个:{{}},如下所示:

     <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 :

但请注意,如果您编写自己的新指令,并且想要将任何表达式传入其中,则必须使用如下的curleyBraces:

    in your controller: 

    $scope.color="blue";

    in your html :

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

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