javascript - Passing data and submit functions between controllers -
basically when user submits calculation done data , stored in variable. had working when in 1 controller, need split between pages. want service. see, i'm confused , know i'm doing wrong. not sure going astray or maybe should better handle on misunderstanding - appreciated. basic pointer nice:
.factory('meals', function() { var data = { baseprice: 0, taxrate: 0, tippercentage: 0, mealcount: 0, tiptotal: 0, averagetip: 0, subtotal: 0, tip: 0, tiptotal: 0, total:0 } function set(data) { saveddata = data; } function get(){ return saveddata; } return { set: set, get: } }) .controller('homectrl', [function(meals) { }]) .controller('mealctrl', [function(meals) { var ws = this; meals.set(); ws.data = meals.get(); ws.submit = function(){ ws.tax_rate= ws.taxrate/100; ws.tip_rate= ws.tippercentage/100; ws.subtotal= ws.baseprice*ws.tax_rate; ws.tip = ws.baseprice*ws.tip_rate; ws.total = ws.baseprice+ws.subtotal+ws.tip; ws.mealcount+=1; ws.tiptotal+=ws.tip; ws.averagetip=(ws.tiptotal+ws.tip)/ws.mealcount; items.tiptotal=ws.tiptotal; items.mealcount=ws.mealcount; items.averagetip=ws.averagetip; }; ws.cancel = function(){ ws.baseprice=''; ws.taxrate=''; ws.tippercentage=''; }; ws.double = function(value){return value*2;}; }]).controller('earningsctrl', [function(meals) {
and html:
<h2>enter meal details</h2> <form name="calculator" ng-submit="ws.submit()"> <p>base meal price:</p> <input type="number" ng-model="ws.baseprice" required> <br> <p>tax rate: %</p> <input type="number" ng-model="ws.taxrate" required> <br> <p>tip percentage: %</p> <input type="number" ng-model="ws.tippercentage" required> <br> <button>submit</button> </form> <button ng-click="ws.cancel()">cancel</button> <h2>customer charges</h2> <p>base price {{ws.data.baseprice | currency}}</p> <p>subtotal {{ws.subtotal | currency}}</p> <p>tip {{ws.tip | currency}}</p> <p>total {{ws.total | currency}}</p>
Comments
Post a Comment