0

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. But I want to use the same code for all operation... Here is html code:

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). Here is javascript line of code:

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

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.