首页 > 解决方案 > 使用 ng-repeat 将参数传递给 ng-click 函数

问题描述

请看下面的代码:

<div>
  <div class="list-group">
    <a class="list-group-item" ng-click="c.selectItem(note.sys_id)" 
       ng-repeat="note in data.notes">
      <h4 class="list-group-item-heading">
        {{note.title}}
      </h4>
      <p class="list-group-item-text">
        {{note.sys_id}}
      </p>
    </a>
  </div>
</div>

在输出中,我可以看到 note.sys_id 正在打印。但是,我需要将其传递给顶部的 ng-click 函数。我尝试了下面的代码,但没有结果:

 ng-click="c.selectItem(note.sys_id)"
 ng-click="c.selectItem({{note.sys_id}})" 

标签: javascriptangularjs

解决方案


https://jsfiddle.net/1q8mdb3z/

ng-click="c.selectItem(note.sys_id)"

应该为你工作。确保你的函数是正确的。我假设你的范围是有效果的

$scope.c = {
    selectItem = function(id) {
      // do something with id
    }
}

推荐阅读