0

My idea is to send data from my html to a function in the angular script, which in turn calls a node script which computes some values, produces a result using a function of itself, and returns values in form of json response back to angular script.

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>

angular script

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';

    });

  };

});

/calculator maps to ./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)

};

The real problem is that the {{output}} does not produce the output as computed in the calculator.js

2 Answers 2

2

You’re not sending the correct data over to 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';

    });

  };
Sign up to request clarification or add additional context in comments.

Comments

0

I think you need to inject $http in myApp.controller('myController', function($scope, $http) { controller and make sure you have inject all required js in html.

i am pasting a code for getting data from url, i may useful for you.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.