I am creating a counting app using the ionic framework. I have a positive button with a predefined number (in services.js) and a corresponding negitive number with the same predefined number to subtract that number. This is the template code;
<a class="item list-inset item-thumbnail-left item item-button-right">
<img ng-src=" {{ menu.imgsrc1 }} ">
<h2>{{ menu.foodItem1 }}</h2>
<p>{{ menu.data1 }}</p>
<button id="plus" class="button button-icon icon round-button" ng-click="count = count + {{ menu.calories1 }} "> {{ menu.calories1 }} </button>
<button id="minus" class="button button-icon icon round-button" ng-click="count = count - {{ menu.calories1 }} "> - </button>
</a>
This is the services code;
var menus = [
{
title: 'Breakfast',
foodItem1: "Cereal",
data1: 'Calories per 250g serving',
imgsrc1: "img/almond.jpg",
calories1: 17,
foodItem2: "Bread",
data2: 'Calories per wholegrain slice',
imgsrc2: "img/almond.jpg",
calories2: 25,
foodItem3: "Pastries",
data3: 'Calories per piece',
imgsrc3: "img/almond.jpg",
calories3: 75
},
{
title: 'Lunch',
description: 'Wrap, Sandwich, Soup...',
foodItem1: 'Wrap',
data1: 'Calories per wholegrain wrap',
imgsrc1: "img/almond.jpg",
calories1: 567,
foodItem2: "Sandwich",
data2: 'Calories per brown slice',
imgsrc2: "img/almond.jpg",
calories2: 567,
foodItem3: "Chicken soup",
data3: 'Calories per 250ml bowl',
imgsrc3: "img/almond.jpg",
calories3: 567
}
];
I do not want the negitive numbers to go below zero. My understanding is that I need to use the Math.floor() method to counteract the button from entering the negative zone but how do I write this without compromising what exists already. Do I need another controller?
I try to understand what you are expecting :
You don't want that number count = count - {{ menu.calories1 }}
to be negative ?
First, I'm never adding data manipulation to HTML. I call a function of my controller that has the data manipulation.
For instance this :
<button id="minus" class="button button-icon icon round-button" ng-click="count = count - {{ menu.calories1 }} "> - </button>
will be changed to
<button id="minus" class="button button-icon icon round-button" ng-click="removeCal(menu.calories1)"> - </button>
and the creation of removeCal function into your controller
$scope.removeCal = function(val) {
$scope.count -= val;
}
Then if you want that number to be >= 0 all the time just add a if rule to manage that case
$scope.removeCal = function(val) {
$scope.count -= val;
if($scope.count <0)
$scope.count = 0;
}