简体   繁体   English

在angularjs中使用不同参数调用同一功能的多个按钮-javascript

[英]Multiple button calling same function with different parameters in angularjs - javascript

I want to call same method in controller on multiple button click with different parameters. 我想在具有不同参数的多个按钮单击上在控制器中调用相同的方法。 If I write different-different javascript code to handle it, works fine. 如果我编写不同的javascript代码来处理它,则效果很好。 But I want to use the same code for all operation... Here is html code: 但是我想对所有操作使用相同的代码...这是html代码:

Note: "breakfastCalorie" and "lunchCalorie" are available dynamic values.

<div id="breakfast">
   <span> Breakfast - {{breakfastCalorie}} </span> 
   <button id="breakfast_btn" ng-click="suggestMeal(breakfastCalorie)">Breakfast</button>
</div>
<div id="lunch">
   <span> Lunch - {{lunchCalorie}} </span> 
   <button id="lunch_btn" ng-click="suggestMeal(lunchCalorie)">Lunch</button>
</div>
... and so on.

And the following function should be called on each button click (or should I write same javascript again). 每次单击按钮时应调用以下函数(或者我应该再次编写相同的javascript)。 Here is javascript line of code: 这是javascript代码行:

$scope.suggestMeal = function () {
   //I want the calorie value to further send it to do some manipulation
}

You can create enum for type of eat 您可以为进餐类型创建枚举

$scope.EatType = {
         breakfast: 1,
         lunch: 2
};

Then call with appropriate type: 然后使用适当的类型调用:

$scope.suggestMeal = function (eatType, cal) {

}

for calling: 致电:

<button id="lunch_btn" ng-click="suggestMeal(EatType.lunch,lunchCalorie)">Lunch</button>

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

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