2015-04-29 32 views
2

我是新來angular.js並有一個簡單的問題。我試圖製作一個計算器,它接受輸入,然後將該輸入值添加到總計中。但是,scope.add函數將不起作用。以下是我的代碼。任何幫助不勝感激。另外角js

的html代碼:

<div class="card"> 
    <div class="item item-text-wrap" style="font-size: 35px; text-align: right"> 
     {{result}} Total 
    </div> 
</div> 

<div class="padding"> 
    <label class="item item-input"> 
    <input class="input-label" placeholder="Add" type="tel" ng-model="T" style="font-size: 35px; text-align: right"> 
    Calories 
    </label> 
</div> 

<div class="row"> 
    <button class="button ion-refresh button-balanced col" ng-click="reset()" style="font-size: 40px"></button> 
    <button class="button ion-plus-round button-calm col" ng-click="add(T)" style="font-size: 40px"></button> 
</div> 

controller.js

angular.module('calorific.controllers', []) 

.controller('calcCtrl', function($scope) { 
$scope.result = 0; 

$scope.reset = function() { 
    $scope.result = 0; 
}; 

$scope.add = function(i) { 
    $scope.result = result + i; 
    return result; 
}; 

}); 
+0

你應該使用'的Number's代替'String's。 – RevanProdigalKnight

+0

有什麼問題,雖然 –

+0

對不起,我沒有關注。我沒有在任何地方使用過String。我認爲。 – daragh

回答

0

,而不是使用.controller angular.controller()。您還應該實例化angular.module()。你不忘記嗎?

結果應該作爲整數而不是字符串傳遞,所以你不能把它作爲引用。

我看不到您的ng-appng-controller指令。你粘貼了你的整個代碼嗎?

+0

對不起,我留下了一些代碼。在此之上,我已經實例化.module,如下所示:angular.module('calorific.controllers',[]) – daragh

0

您需要實例化變量T在這樣的控制器:

$scope.T = 0;