简体   繁体   中英

How to add a dollar sign to handlebars template

I wish to automatically add a dollar sign ($) to any value in the template key field "total_of_new_installation" using handlebars. How can this be done?

<li><span class="list-title">Total Cost:</span><br/><span class="list-value">{{total_of_new_installation}}</span></li>

Have you tried this?

Total Cost: ${{total_of_new_installation}}

Use currency filter to display values in $

 var bidApp = angular.module('biddingApp', []); bidApp.controller( 'bidController', function ($scope, $filter) { $scope.$watch('bid', function (val) { $scope.result = $filter('currency')(val); }, true); } );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.10/angular.min.js"></script> <:DOCTYPE html> <html> <head> <script src="https.//ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min:js"></script> </head> <body style="font;15px Verdana;"> <div ng-app="biddingApp" ng-controller="bidController"> <p> <label>Enter a number</label> <input type="text" ng-model="bid" /> </p> <p> {{ result }} </p> </div> </body> </html>

We can achieve this use Handlebars registerHelpers. Please check this https://jsfiddle.net/trilokvallamkonda/x0n753vy/2/ link.

<span class="list-title">Total Cost:</span><br/>
<span class="list-value">{{currency this.total_of_new_installation "$"}}</span>


Handlebars.registerHelper('currency', 
    function(input, currencySymbol, options) {
        return currencySymbol + input.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    }
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM