2013-10-29 37 views
1

我創建了一個小型計算器,它創建了多個綁定值。有一個更好的方法嗎?我覺得這是徹頭徹尾的黑客。我是Angular的新手。Angular - 檢查數據是否存在?

計算器:

<div ng-app="App" ng-controller="CalculatorCtrl"> 
    <form action="javascript:;" id="calculator" class="form-inline"> 
     <fieldset class="grid grid-4"> 
      <div class="item"> 
       <input required type="number" 
        ng-model="data.buy" 
        placeholder="Buy Price" 
        maxlength="10" 
        tabindex="1" /> 
      </div> 

      <div class="item"> 
       <input required type="number" 
        ng-model="data.sell" 
        placeholder="Sell Price/target" 
        maxlength="10" 
        tabindex="2" /> 
      </div> 

      <div class="item"> 
       <input required type="number" 
        ng-model="data.shares" 
        placeholder="Share Count" 
        maxlength="10" 
        tabindex="3" /> 
      </div> 

      <div class="item"> 
       <input required type="number" 
        ng-model="data.fee" 
        placeholder="Commission Fee" 
        maxlength="10" 
        tabindex="3" /> 
      </div> 
     </fieldset> 
    </form> 

    <!-- Results --> 
    <table class="table table-bordered table-striped table-hover"> 
     <tr> 
      <th width="150">Total Profit</th> 
      <td>{{ profit() | number:2 }}%</td> 
     </tr> 

     <tr> 
      <th width="150">Net Gain</th> 
      <td>{{ data.net = (data.soldFor - data.purchasedFor) | currency }}</td> 
     </tr> 

     <tr> 
      <th width="150">Purchased For</th> 
      <td>{{ data.purchasedFor = (data.buy * data.shares) + data.fee | currency }}</td> 
     </tr> 

     <tr> 
      <th width="150">Sold For</th> 
      <td>{{ data.soldFor = (data.sell * data.shares) - data.fee | currency }}</td> 
     </tr> 
    </table> 
</div> 

的JS(CoffeeScript的)問題:

# Core Coffee 
app = angular.module 'App', [] 
app.controller 'CalculatorCtrl', ['$scope', ($scope) -> 
    $scope.profit = -> 
     data = $scope.data 
     return 0 unless data? and data.net? and data.purchasedFor? and data.soldFor? 

     a = if data.soldFor > data.purchasedFor then data.soldFor else data.purchasedFor 
     b = if a is data.soldFor then data.purchasedFor else data.soldFor 

     res = if data.net >= 1 then '+' else '-' 
     res += ((a - b)/b) * 100 
] 

我怎樣才能確保數據存在(如自動如何角做),而這條線? return 0 unless data? and data.net? and data.purchasedFor? and data.soldFor?

編輯:由於馬克西姆,這完美的作品:

### Watch for the net gain value and then calculate a profit % ### 
$scope.$watch 'data.net', (newVal, oldVal) -> 
    return 0 unless newVal? 
    data = $scope.data 

    a = if data.soldFor > data.purchasedFor then data.soldFor else data.purchasedFor 
    b = if a is data.soldFor then data.purchasedFor else data.soldFor 

    $scope.data.profit = if newVal >= 1 then '+' else '-' 
    $scope.data.profit += ((a - b)/b) * 100 
+0

無法迴避的計算器中測試你的屬性。你可以把這些屬性放到一個數組和循環數組中進行測試,如果任何一個不存在,則返回零。或者在'data'中循環鍵,所以不需要硬編碼屬性名稱 – charlietfl

回答

1

我會用$watch聽上data

見文檔here

一般$watch與標誌true不深的比較。

喜歡的東西(希望你可以將其轉換爲咖啡)

$scope.$watch(function() { 
    return$scope.data; 
    }, 
    function (newValue, oldValue) { 
    // here you can write any action 
    }, true); 
+0

仍然只能填寫一個字段,手錶將會觸發。仍然必須驗證屬性 – charlietfl

+0

'watch'將會在數據將發生變化時觸發,無論哪個子字段 –

+0

我認爲是這樣,因爲我可以監視「data.net」,它無論如何都需要其他所有東西。將測試。編輯:它的作品! –