Tips for customizing the appearance of currency in Angular using ngBind

Currently, I am attempting to achieve this design using angularjs and css: https://i.sstatic.net/l1wv6.png

The specific requirement is to display the decimal part of a number in the upper right corner, similar to what's shown in the image. While ng-bind can align it normally, I'm seeking suggestions on how to accomplish this unique layout. Any insights or ideas would be greatly appreciated. Thank you.

Answer №1

Setting Currency Value

$scope.model.currency = 10.20;

Implementing Directive

<div gl-currency="model.currency"></div>

Custom Directive Code

.directive('glCurrency', [function()
{
  return {
    scope: 
    {
      glCurrency: '='
    },
    restrict: 'A',
    templateUrl: 'currency.html',
    link: function($scope, element, attrs)
    {
      $scope.$watch('glCurrency', function(currency)
      {
        if(currency)
        {
          var split = currency.toString().split('.');

          if(Array.isArray(split))
          {
            $scope.number.dollars = split[0] ? split[0] : '0';
            $scope.number.cents = split[1] ? split[1] : '00';
          }
        }
      });
    }
  };
}]);

Display Template for Directive

<div class="currency">
  <span class="currency__dollars">{{ number.dollars }}</span>
  <span class="currency__cents">{{ number.cents }}</span>
</div>

Styling for the Currency Display

.currency {
  position: relative;
}

.currency__dollars {
  font-size: 24px;
  padding-right: 10px; 
}

.currency__cents {
  font-size: 10px;
  position: absolute;
  top: 0;
  right: 0;
}

Answer №2

If you want to achieve this, it's important to separate the two parts. One way to do this is by using a directive:

.directive("separatedCurrency", function() {
    return {
        restrict: "AE",
        scope: {
            amount: "=",
            symbol: "@",
            fraction: "="
        },
        template: "<span class='currency-main' ng-bind='amount | currency:symbol:0'></span><span class='currency-decimals' ng-bind='decimals'></span>",
        link: function (scope, element, attrs) {
            scope.fraction = scope.fraction || 2; // default 2 decimals
            scope.decimals = (scope.amount % 1) * (10^scope.fraction);
        }
    }
}

This method allows you to apply different styles to currency-main and currency-decimals using CSS.

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

Retrieve data from an AngularJS scope within Directives

I am facing an issue with accessing a scope variable from the app controller in order to update it on a click event triggered by a custom Directive. It appears that the isolated scope of the Directive is unable to access the global scope. The scope variab ...

Troubleshooting CSS3 pulse animation failure in Internet Explorer and Firefox

My animated background is looping infinitely and working perfectly in Chrome and Safari. However, I'm having trouble getting it to work in IE and FF. I've tried looking at other people's questions on this topic but still can't figure it ...

What is the best way to send a JavaScript variable to PHP for storage in a WordPress database?

I am currently in the process of developing a star rating system within Wordpress and am looking to store the rating data in the Wordpress database. To achieve this, I have saved the star rating PHP code as a template within my Wordpress theme folder. Belo ...

Center your logo and position the text to the left in your Bootstrap 5

I am struggling to align my navigation bar with the toggler button on the left and the brand logo in the center, similar to the image provided below: https://i.sstatic.net/k34m9.png I am having difficulty achieving this layout. Currently, my code display ...

Adjust the height of a div using jQuery once the AJAX call retrieves the data

There is a div that always matches the height of the adjacent div, depending on the content loaded in it. This setup was functioning properly until I implemented a search feature using jQuery. Now, when I perform a search, the element used in the jQuery ca ...

What are the drawbacks of utilizing CSS word-wrap across all elements on a website?

Is there a downside to utilizing the CSS property word-wrap:break-word in this manner? body { word-wrap: break-word; } The definitions for the values of this property are as follows: normal: Breaks words only at allowed breakpoints break-word: Perm ...

Nested dropdown within a parent container with scrollable overflow

As part of a project, I am required to design a view where a drop-down menu is positioned at the bottom of a parent div. You can see an example here: http://jsfiddle.net/Gruck/6owhaa63/. The "move to" item expands to show more options, similar to the mocku ...

Position an image in the center and add a div below it

I am seeking guidance regarding two specific questions. My objective is to showcase an image in its true size while positioning it at the horizontal center of the screen (not the exact center), followed by a div containing text directly underneath the imag ...

Secondary menu supersedes primary dropdown menu

Having trouble getting a vertical dropdown menu to work properly, with the submenus overriding the main menu. I've searched everywhere for a solution but can't seem to find one. Anyone out there who could help me figure this out? Here's the ...

AngularJS - ng-repeat: Warning: Repeated items found in the repeater and are not allowed. Repeater:

I'm currently using ng-repeat to showcase a collection of items fetched from the Twitter API. However, I am encountering an issue where Angular attempts to display the empty list while the request is still being processed, resulting in the following e ...

"Learn the process of transferring data from one controller to another in your application

After adding categoryCtrl and ProductCtrl, the code line: console.log(category); is functioning correctly. I am looking for a way to bind this data to the product controller using a for loop or any other method. .controller('CategoryCtrl', funct ...

Revamping the Bootstrap framework in a major project

Currently, I am undertaking a project that involves the use of bootstrap 2.3.2 stylesheets along with bootstrap 3.0.0 .js scripts. The client side technology is significantly outdated and my primary objective is to update the bootstrap files to leverage th ...

jQuery mobile : accessibility helper for hidden elements

Within my HTML structure: <div data-role="fieldcontain" id="containdiv" class="no-field-separator"> <label for="field1" class="ui-hidden-accessible">To</label> <input type="search" name="field1" id="field1" autocorrect="off" a ...

"Troubleshooting the issue of Angular UI-Select failing to display a large

Check out my comprehensive json file containing cities from around the world: download here. Furthermore, take a look at the snippet of html code below: <div class="form-group"> <label class="control-label"> CITY </label> ...

Encountering an issue in AngularJS where a necessary dependency cannot be resolved while attempting to utilize a global service

In order to streamline my code, I decided to create a service for a function that is utilized in multiple controllers. To achieve this, I added a commonService.js file to my index.html 'use strict'; var myApp = angular.module('myAppName&ap ...

Maintain the text layout when copying and pasting from the Angular Application

After noticing that copying and pasting text from an Angular Application to a text editor like Microsoft Word results in losing the original format, I decided to investigate further. An example I used was the angular material website: https://material.ang ...

What is the best way to target an HTML element that is only connected to a particular class?

My HTML code includes the following 3 elements: <span class="a b"></span> <span class="a"></span> <span class="a b"></span> I am trying to select the element that only has the class "a". When I use $("span.a"), it sele ...

Update the class of the appropriate navigation tab when the corresponding div is scrolled into view

After reading similar questions and doing some research on scrollspy, I don't think it will provide the functionality I need. It seems to only support bootstrap style highlighting. If there is more to it that I'm not aware of, please inform me! ...

Learning how to effectively incorporate two matSuffix mat-icons into an input field

I'm currently experiencing an issue where I need to add a cancel icon in the same line as the input field. The cancel icon should only be visible after some input has been entered. image description here Here's the code I've been working on ...

Is it possible to utilize Cordova for transitioning a web application to a native mobile app?

Is it possible to use Cordova to package and deploy my SPA application, which is tailored for mobile browsers and built using Angular 2 and Node, as a native app on both Android and iOS devices? Are there any resources or references available that can gui ...