2016-09-30 4 views
0

私の考えは、自分のhtmlからangleスクリプト内の関数にデータを送ります。角度スクリプトは、いくつかの値を計算し、自身の関数を使って結果を生成するノードスクリプトを呼び出し、 jsonレスポンスの形式で値を返し、角度スクリプトに戻します。角度から私のnodejsに値を取得できません

HTML

<div ng-app="myApp" ng-controller="myController" ng-init='isFocused=true'> 
    <div class="col-lg-5 col-md-8 col-sm-12 col-xs-12"> 
     <input width="100%" type="text" class="form-control" id="operand1" name="operand1" style="height:50px; font-size:32px" ng-model="operand1" ng-focus=' isFocused="operand1" ' autofocus> 
    </div> 

    <div class="col-lg-2 col-md-8 col-sm-12 col-xs-12 text-center" style="font-size:32px"> 
     {{op}} 
    </div> 

    <div class="col-lg-5 col-md-8 col-sm-12 col-xs-12"> 
     <input width="100%" type="text" class="form-control" style="height:50px; font-size:32px" ng-model="operand2" ng-focus=' isFocused="operand2" ' id="operand2" name="operand2" autofocus> 
    </div> 


</div> 


<div class="col-lg-5"> 

    <div class="row"> 

     <div class="col-lg-3 col-md-3 col-sm-12 col-xs-12 eqProps text-center"> = </div> 

     <div class="col-lg-9 col-md-9 col-sm-12 col-xs-12"style="font-size:32px" ng-model="output"> 
      {{output}} 
     </div> 
    </div> 

角度スクリプト

var myApp = angular.module('myApp', []); 

myApp.controller('myController', function($scope) { 

    $scope.operators = { 
    '+': add, 
    '-': sub, 
    '*': mul, 
    '/': div 
    }; 
    $scope.op = '+'; 
    $scope.calc = calc; 


    $scope.submit = function() { 

    $http({ 
     method : "POST", 
     url : '/calculator', 
     data : { 
     "answer" : $scope.answer 
     } 
    }).success(function(data) { 

     $scope.output = $scope.operand1 + $scope.operand2; 

    }).error(function(error) { 

     $scope.output = 'Invalid Input'; 

    }); 

    }; 

}); 

/電卓は

を./routes/calculator.computeにマッピング

calculator.js

exports.compute = function(req, res){ 

    var operand1 = req.body.operand1; 
    var operand2 = req.body.operand2; 
    var operator = req.body.operator; 


    var json_responses; 

      json_responses = {"answer" : operand1+operand2}; 
      res.send(json_responses) 

}; 

本当の問題は、あなたが正しいを送信していない

答えて

2

calculator.jsに計算された{{出力が}}出力を生成しないということですデータをnodejsに渡す。

$scope.submit = function() { 

    $http({ 
     method : "POST", 
     url : '/calculator', 
     data : { 
     "operand1" : $scope.operand1, 
     "operand2" : $scope.operand2, 
     "operator" : $scope.op 
     } 
    }).success(function(data) { 

     $scope.output = data.answer; 

    }).error(function(error) { 

     $scope.output = 'Invalid Input'; 

    }); 

    }; 
0

私はあなたが$httpmyApp.controller('myController', function($scope, $http)で{コントローラを注入して、HTMLで必要なすべてのJSを注入していることを確認する必要があると思います。

URLからデータを取得するためのコードを貼り付けています。あなたにとって役に立ちます。

function functionName() { 
      $http.get('URL').success(function (response) { 
       $scope.variableName = response; 
      }) 
     } 
関連する問題