Angularjs 无法从angular到my NodeJ获取值
我的想法是将数据从html发送到angular脚本中的一个函数,该函数反过来调用一个节点脚本,该脚本计算一些值,使用自身的函数生成结果,并以json响应的形式将值返回给angular脚本 htmlAngularjs 无法从angular到my NodeJ获取值,angularjs,node.js,Angularjs,Node.js,我的想法是将数据从html发送到angular脚本中的一个函数,该函数反过来调用一个节点脚本,该脚本计算一些值,使用自身的函数生成结果,并以json响应的形式将值返回给angular脚本 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">
<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>
/calculator映射到./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)
};
真正的问题是,{output}}没有产生计算器中计算的输出。js您没有将正确的数据发送到nodejs
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)
};
$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';
});
};
您没有将正确的数据发送到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';
});
};
我认为您需要在
myApp.controller('myController',function($scope,$http)
{controller)中插入$http
,并确保已在html中插入所有必需的js
我粘贴了一个从url获取数据的代码,我可能对你有用
function functionName() {
$http.get('URL').success(function (response) {
$scope.variableName = response;
})
}
我认为您需要在
myApp.controller('myController',function($scope,$http)
{controller)中插入$http
,并确保已在html中插入所有必需的js
我粘贴了一个从url获取数据的代码,我可能对你有用
function functionName() {
$http.get('URL').success(function (response) {
$scope.variableName = response;
})
}