2016-09-30 57 views
0

我的想法是將數據從我的html發送到角度腳本中的函數,後者又調用計算某些值的節點腳本,使用其自身的函數產生結果,並以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) 

}; 

真正的問題是,{{輸出}}不會產生輸出計算在calculator.js

回答

2

你不發送正確數據傳到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

我覺得你需要注入在myApp.controller('myController', function($scope, $http) {控制器$http,並確保你有注入所有必需的JS在HTML中。

我正在粘貼從url獲取數據的代碼,我可能對您有用。

function functionName() { 
      $http.get('URL').success(function (response) { 
       $scope.variableName = response; 
      }) 
     }