Advertisement
mhyusuf

sample angularjs invoice with currency

May 26th, 2015
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en-US">
  3.     <!--
  4.         Belajar Angular JS dengan praktek 3
  5.         invoice dan currency
  6.         tema: filter
  7.         Yoesoff / Yusuf
  8.         blog : undebugable.wordpress.com
  9.         penjelasan :  Belajar praktek angularJS
  10.      -->
  11.     <head>
  12.         <!-- Load Angular -->
  13.         <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
  14.     </head>
  15.    
  16.     <body>
  17.         <div ng-app="invoice1" ng-controller="InvoiceController as invoice">
  18.               <b>Invoice:</b>
  19.            
  20.               <div>
  21.                 Quantity: <input type="number" min="0" ng-model="invoice.qty" required >
  22.               </div>
  23.              
  24.               <div>
  25.                 Costs: <input type="number" min="0" ng-model="invoice.cost" required >
  26.                 <select ng-model="invoice.inCurr">
  27.                   <option ng-repeat="c in invoice.currencies">{{c}}</option>
  28.                 </select>
  29.               </div>
  30.              
  31.               <div>
  32.                 <b>Total:</b>
  33.                 <span ng-repeat="c in invoice.currencies">
  34.                   {{invoice.total(c) | currency:c}}
  35.                 </span>
  36.                 <button class="btn" ng-click="invoice.pay()">Pay</button>
  37.               </div>
  38.         </div>
  39.     </body>
  40.    
  41.     <script>
  42.     var app = angular.module('invoice1', []);
  43.     app.controller('InvoiceController', function() {
  44.           this.qty = 1;
  45.           this.cost = 2;
  46.           this.inCurr = 'EUR';
  47.           this.currencies = ['USD', 'EUR', 'CNY'];
  48.          
  49.           this.usdToForeignRates = {
  50.             USD: 1,
  51.             EUR: 0.74,
  52.             CNY: 6.09
  53.           };
  54.    
  55.           this.total = function total(outCurr) {
  56.             return this.convertCurrency(this.qty * this.cost, this.inCurr, outCurr);
  57.           };
  58.           this.convertCurrency = function convertCurrency(amount, inCurr, outCurr) {
  59.             return amount * this.usdToForeignRates[outCurr] / this.usdToForeignRates[inCurr];
  60.           };
  61.           this.pay = function pay() {
  62.             window.alert("Thanks!");
  63.           };
  64.     });
  65.    
  66.     </script>
  67.    
  68. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement