如何在ng-repeat中使用ng-click?

问题描述 投票:1回答:2

我是Angular JS的新手,我试图使用ng-repeat在我的表中生成多行,并且每行中应该都有一个按钮来触发具有不同参数的函数

<div ng-controller="testing">
    <table>
        <thead>
             <tr>
                  <th>col 1</th>
                  <th>col 2</th>
                  <th>button</th>        
             </tr>
        </thead>
        <tbody>
             <tr ng-repeat="item in items">
                 <td>{{ item.prop1 }}</td>
                 <td>{{ item.prop2 }}</td>
                 <td><button ng-click="myFunction(item.id)">Trigger Function</button></td>
             </tr>
        </tody>
    </table>
</div>

这当然是行不通的,因为ng-repeat创建了自己的作用域,而myFunction是在父作用域(即测试控制器)中定义的。

但是,我在Google结果中只能找到无法正常工作的原因,但缺乏解决问题的方法。我应该如何纠正我的代码,以便ng-click起作用?有没有办法在ng-repeat范围内定义myFunction,还是有办法告诉我想要在父范围内的函数?

javascript angularjs angularjs-scope angularjs-ng-repeat
2个回答
0
投票

ng-click更改为以下内容


0
投票

提高ng-repeat性能–使用ng-repeat跟踪功能

© www.soinside.com 2019 - 2024. All rights reserved.