Book Image

AngularJS Web Application Development Blueprints

By : Vinci J Rufus
Book Image

AngularJS Web Application Development Blueprints

By: Vinci J Rufus

Overview of this book

Table of Contents (17 chapters)
AngularJS Web Application Development Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Summarizing the expenses by categories


Getting back to our application, as we want to create a graph showing the summary of expenses for each category, we will first need to calculate the total expense made under each category.

We will start by creating a function within our factory service that will total up the expenses for a category name that is passed to it.

Open up the app/js/services.js file, and add the following function within the return {} section of the expService factory:

getCategoryTotal: function(category) {
  var categoryTotal = 0;
  var prefixLength = prefix.length;
  Object.keys(localStorage)
    .forEach(function(key) {
      if (key.substring(0, prefixLength) == prefix) {
        var item = localStorage[key]
        item = JSON.parse(item)
        if (item.category == category) {
          categoryTotal += parseFloat(item.amount);
        }

      }

    });
  return categoryTotal;
}

Tip

For the sake of simplicity, we are directly creating our methods within the return statement...