2016-06-27 13 views
0

私のコードでどこが間違っていましたか?ボタンをクリックすると、最初の番号と2番目の入力番号が表示されます。モデルをanglejsのコントローラ関数に渡す

app.controller('AddSum', ['$scope', function ($scope) { 
 
    $scope.sum = 0; 
 
    $scope.add = function (fnumber, snumber) { 
 
     $scope.a = $scope.fnumber; 
 
     $scope.b = $scope.snumber; 
 
     $scope.sum = $scope.a + $scope.b; 
 
     
 
    }; 
 
}]);

Htmlの

<table ng-controller="AddSum"> 
 
      <tr> 
 
       <td>Enter First Number:</td> 
 
       <td><input ng-model="fnumber"></td> 
 
      </tr> 
 
      <tr> 
 
       <td>Enter Second Number:</td> 
 
       <td><input ng-modle="snumber" /></td> 
 
      </tr> 
 
      <tr> 
 
       <td><button ng-click="add()">SUM</button></td> 
 
       <td><p>Sum:{{sum}}</p></td> 
 
      </tr> 
 
      
 
    </table>


そのが機能していない、変更されたコードを参照してください。

    
 
       app.controller('AddSum', ['$scope', function ($scope) { 
 
    $scope.sum = 0; 
 
    $scope.add = function (fnumber,snumber) { 
 
     $scope.a = $scope.fnumber; 
 
     $scope.b = $scope.snumber; 
 
     $scope.sum = parseInt($scope.a) + parseInt($scope.b); 
 
     
 
    }; 
 
}]);
<table ng-controller="AddSum"> 
 
      <tr> 
 
       <td>Enter First Number:</td> 
 
       <td><input ng-model="fnumber"></td> 
 
      </tr> 
 
      <tr> 
 
       <td>Enter Second Number:</td> 
 
       <td><input ng-model="snumber" /></td> 
 
      </tr> 
 
      <tr> 
 
       <td><button ng-click="add(fnumber,snumber)">SUM</button></td> 
 
       <td><p>Sum:{{sum}}</p></td> 
 
      </tr> 
 
    </table>

答えて

0

私は解決策を得ました。

app.controller( 'AddSum1'、関数($スコープ){

$scope.add = function (num1, num2) { 
    $scope.sum = parseInt(num1) + parseInt(num2); 
}; 

})。

0

あなたはタイプミスを持っています。

変更

<td><input ng-modle="snumber" /></td> 

<td><input ng-model="snumber" /></td> 

は、その後、あなたが望むだろうが、 $scope.sum = $scope.a + $scope.b;$scope.sum = parseInt($scope.a) + parseInt($scope.b); は、トリックを行います変更番号

として入力を解析する必要がありますもう少しelをする例。

関連する問題