Issue with filtering of values returned by functions

I've been working with Angular's currency filter and I've run into an issue. It doesn't seem to be filtering the data that is returned from a function. I have a feeling that I might be missing something, perhaps it has to do with how the filter gets digested?

Here are some snippets of my code:

HTML template file

<span>total: {{ vm.total.exgst | currency}}<span>

Controller.js for the HTML template

var calculateTotalExGST = function() { return _.sum(vm.items, function(item)
                                                   { return item.cost; });
                                      };
vm.total = { exgst: calculateTotalExGST() };

Currently, the HTML displays

total: 5

But ideally, it should display as $5.00. The value is being returned by the function, but the currency filter doesn't seem to work. Could it be related to when $digest happens?

Can anyone provide any insights on this issue?

Edit: Just to provide more context about the code, vm.items are values coming from $state in ui-router.

I'm not sure how to replicate all of this in jsfiddler. So I've simply mocked $state. Unfortunately, I'm unable to reproduce my issue there.

https://jsfiddle.net/ex5rj9u7/

Answer №1

It appears that the method you are using may not be correct in this context. According to the Lodash documentation, the sum function is typically used for arrays. You might want to consider using the sumBy method instead.

Here is an example for reference: https://jsfiddle.net/relferreira/dng7s3qs/

JavaScript:

angular.module('app', []);

angular.module('app')
    .controller('MainController', mainController);

mainController.$inject = ['$scope'];

function mainController($scope){

    var vm = this;

  vm.items = [
    { cost: 5},
    { cost: 5}
  ];

    var calculateTotalExGST = function() { 
    return _.sumBy(vm.items, function(item){ 
        return item.cost; 
    });
  };

  console.log(calculateTotalExGST())

    vm.total = { exgst: calculateTotalExGST() };


}

HTML:

<div data-ng-app="app">

  <section ng-controller="MainController as mainVm">
    <span>total: {{ mainVm.total.exgst | currency}}</span>
  </section>

</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to determine if a specific string matches a value within an object?

Within my codebase, there exists an object named "codes": var codes = { code1: 'test1' code2: 'test2' } I am looking to determine if this object possesses a specific property and then display the result in the console. if(inp ...

Issue-free AJAX call to Neo4j database on local server with no 'Access-Control-Allow-Origin' problem

I'm currently working on a basic JavaScript AJAX request to connect from a MAMP server running at localhost:8888 to a Neo4j database running on localhost:7474. The issue I'm encountering is the following error message: XMLHttpRequest cannot l ...

React web app experiencing an issue with MUI Drawer opening flawlessly but failing to close

Recently, I encountered an issue with my React app. I have a Navbar component that displays a Sidebar component when the screen is small, using Material UI for the Drawer functionality. The problem arises when clicking the hamburger icon to open the drawe ...

Problem with deleting or substituting symbols and character codes within a String

I am attempting to send an object called dataO from Node.js/Express/EJS to the client side. On the Node.js script side: var dataO = {"first":[20000, 14000, 12000, 15000, 18000, 19000, 22000], "second":[12000, 11000, 18000, 12000, 19000, 14000, 26000]}; var ...

Render function in Next.js did not yield anything

Recently, I came across the next.js technology and encountered an error. Can anyone help me solve this issue? What could be causing it?View image here import React from 'react' import Button from "../components/button" function HomePa ...

Looking to notify the second set of JSON data arrays?

Today, I encountered an issue while working with two arrays of JSON data. Upon trying to alert the second JSON data in AJAX, I found that it was showing as "undefined". How can I successfully alert the value of the second JSON data? The relevant portion o ...

Issue in CakePHP after modifying the directory of Index

I'm currently working on a project using CakePHP and have come across an issue. Our team developed an Ajax function that sends data to a PHP function responsible for adding a folder (known as "ordner" in German) to the database. Initially, everything ...

Switching from HTML to BBCode during the editing process

My issue lies in converting BBCode to HTML and then editing the code on a page with AJAX. When editing in a <textarea>, the HTML tags show up instead of the original BBCode. For instance, if I submit [b]bold text[/b] and save it to my database, the ...

Send in the completed form with your chosen preferences

Is there a way for me to submit a form with the selected option? My backend code is set up to work when using inputs: <input id="id_value_0" name="value" type="radio" value="1" /> <input id="id_value_1" name="value" type="radio" value="2" /> ...

Tips for displaying personalized data with MUI DatePicker

I need to create a React TypeScript component that displays a MUI DatePicker. When a new date is selected, I want a custom component (called <Badge>) to appear in the value field. Previously, I was able to achieve this with MUI Select: return ( ...

Having trouble integrating Ionic Native API and Javascript API in one application?

Currently, I am developing an application using Google Maps that utilizes both the Ionic Native Google Maps API and the JavaScript version. The Native API is implemented on a page called home.ts, while the JavaScript API is utilized on a sub-page called de ...

Steps for adding information to an AngularJS scope

I am facing an issue with setting the data inside the scope once it becomes active. On my HTML page, I have a show/hide menu that displays data when a button is clicked. I need to store this data within the scope correctly. Please advise on any corrections ...

Navigate JSON Object - Prior Action

I am looking for a solution related to the following question/answer: How can I cycle through JSON objects one by one in AngularJS on click I need help with a function that displays the previous item from a JSON object. When the first item is selected, c ...

How to Use jQuery in Thymeleaf to Toggle All Checkboxes

Why isn't the function of selecting and deselecting all fields working correctly for me? Can anyone explain what may be causing this issue? ..... <head> <meta name="viewport" content="width=device-width, initial-scale=1.0&q ...

Should I deploy the Angular2 demo app within Rails or as its own standalone application?

Currently diving into the world of Angular2 and completing both the quickstart and heroes tutorial. Each time I start these applications, I use the "npm start" command. On top of that, I've developed a Ruby on Rails backend application alongside an A ...

safely sending different form inputs in a Ruby on Rails form

Within this snippet, you'll find a part of a table with a form. Each row has its own submit button, which means users have to click on each one individually. I'm currently working on changing this so there is just one button for submission. < ...

Inexplicably, HighCharts flips its axis when exporting data

I am facing an issue with the display of my Highchart in the web application. While it appears correctly in the browser, the exported chart flips the axis and shows a different image. Below are the options I have configured for my Highchart: var options = ...

React version 18.2.0 and Next.js version 13 faced an issue with hydration failure due to the initial user interface not matching. This problem

Upon opening the page, I encounter an error when focusing on the input element right away. However, if I wait a few seconds before focusing on the input element, the error does not occur. Unfortunately, I have been unable to resolve this issue. Interesting ...

I am facing an issue with my react-app where it compiles successfully without any errors, but it is not rendering any elements

JavaScript file to run with npm start: import React from 'react'; import ReactDOM from 'react-dom'; import { BrowserRouter as Router } from 'react-router-dom'; import Routes from './routes'; ReactDOM.render( <R ...

Using Vue to create a component that binds an array as its data source

Having trouble binding an array as a variable and accessing it in the child component, resulting in the variable being undefined. Is there a way to pass an array from a view to a component so that the component can use this array to create a child componen ...