多个按钮调用与angularjs中不同参数相同的函数 - javascript

时间:2017-09-19 10:25:10

标签: javascript jquery angularjs

我想在控制器上使用不同的参数点击多个按钮来调用相同的方法。如果我编写不同的javascript代码来处理它,工作正常。但我想对所有操作使用相同的代码...这是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.

每次点击按钮都应该调用以下函数(或者我应该再次编写相同的javascript)。 这是javascript代码行:

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

1 个答案:

答案 0 :(得分:1)

您可以为吃的类型创建枚举

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

然后使用适当的类型调用:

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

}

致电:

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