How to pass parameters to $http in angularjs?

前端 未结 4 1480
囚心锁ツ
囚心锁ツ 2020-12-04 23:45

Assume that I have two input boxes with corresponding ng-model as fname and lname. If I call http request as :

$http({method:\'GET\', url:\'/search\', params         


        
4条回答
  •  無奈伤痛
    2020-12-05 00:16

    We can use input data to pass it as a parameter in the HTML file w use ng-model to bind the value of input field.

    
    
     
    

    and in the js file w use $scope to access this data:

    $scope.email="";
    $scope.password="";
    

    Controller function will be something like that:

     var app = angular.module('myApp', []);
    
        app.controller('assignController', function($scope, $http) {
          $scope.email="";
          $scope.password="";
    
          $http({
            method: "POST",
            url: "http://localhost:3000/users/sign_in",
            params: {email: $scope.email, password: $scope.password}
    
          }).then(function mySuccess(response) {
              // a string, or an object, carrying the response from the server.
              $scope.myRes = response.data;
              $scope.statuscode = response.status;
    
            }, function myError(response) {
              $scope.myRes = response.statusText;
          });
        });
    

提交回复
热议问题