繁体   English   中英

AngularJS,如何将范围变量传递给在ng-repeat内部更改的js函数?

[英]Angularjs, how to pass a scope variable to js function which changes inside ng-repeat?

我的html中有一个ng-repeat循环。 循环中涉及的变量显示在表行中。 我需要允许用户单击所需的值,并让js函数接收该值,以便它可以对其执行操作。 我的代码的早期版本不尝试传递值,而只是显示它,它在这里并且可以正常工作; 显示带有链接属性的filteredList中的各种主机值(在我的情况下,带下划线)

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

我试图将用户单击的主机的值传递给我的函数“搜索器”,但这不起作用:

<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会抱怨:错误:[$ parse.syntax]语法错误:令牌'update.host'意外,期望表达式[searcher({{update.host}})的第12列有[:]; ]从[update.host}} ;;]开始。

有人可以告诉我angularjs可接受的一种方法来将单击的主机值传递给我的函数吗?

非常感谢。

正如其他人在评论中提到的那样,这将为您工作:

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

查阅ng-click的文档 ,这表明它接受表达式 (因此您不需要{{binding}}语法)。

这是正确的方法

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

尝试:

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

如前所述,您无需在ng-click内的变量周围使用大括号。 与此无关,在任何角度指令参数中都没有。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM