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

If you are a web application developer interested in using AngularJS for a real-life project, then this book is for you. As a prerequisite, knowledge of JavaScript and HTML is expected, and a working knowledge of AngularJS is preferred.
Table of Contents (12 chapters)
11
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...