如何在一次 ng-click 中添加许多功能? [英] How to add many functions in ONE ng-click?

查看:16
本文介绍了如何在一次 ng-click 中添加许多功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找如何执行此操作,但到目前为止我找不到任何相关内容,:(我可以嵌套这两个函数是的,但我只是想知道这是否可能?我想从字面上做到这一点:

I've be looking for how to execute this but I can't find anything related so far, :( I could nest both functions yes but I'm just wondering if this is possible? I'd like to do this literally:

<td><button class="btn" ng-click="edit($index) open()">Open me!</button></td>

我现在的 JS 代码:

My JS code at the moment:

$scope.open = function () {
  $scope.shouldBeOpen = true;
};      

$scope.edit = function(index){

  var content_1, content_2;
      content_1 = $scope.people[index].name;
      content_2 = $scope.people[index].age;

  console.log(content_1);
};

我想一键调用两个函数,我该如何在 angularJS 中做到这一点?我认为当您添加多个类时,它会像在 CSS 中一样直接......但事实并非如此:(

I'd like to call two functions with just one click, how can I do this in angularJS? I thought it'd be straight forward like in CSS when you add multiple classes...but it's not :(

推荐答案

您有 2 个选择:

  1. 创建包装这两个方法的第三个方法.这里的优点是您在模板中放置的逻辑更少.

  1. Create a third method that wrap both methods. Advantage here is that you put less logic in your template.

否则,如果您想在 ng-click 中添加 2 个调用,您可以添加 ';'edit($index) 像这样

Otherwise if you want to add 2 calls in ng-click you can add ';' after edit($index) like this

ng-click="edit($index); open()"

见这里:http://jsfiddle.net/laguiz/ehTy6/

这篇关于如何在一次 ng-click 中添加许多功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆