Angular JS在HTTP post请求后分配范围变量

时间:2017-03-12 05:48:36

标签: javascript angularjs

我无法在HTTP post请求后分配范围变量。

控制器:

storeApp.controller("formCtrl", ['$scope', '$http', function ($scope, $http) {          
  $scope.variableToBeAssigned= null;
}

$scope.formsubmit = function () {
         $http.post($scope.url, { "name": $scope.name}).
                    success(function (data, status){
                        $scope.variableToBeAssigned= "success";                            
                    })}

当用户提交表单时,将调用formsubmit()函数。即使在HTTP Post请求之后,“variableToBeAssigned”仍然为空。

1 个答案:

答案 0 :(得分:-1)

您应该将该功能放在控制器内,

storeApp.controller("formCtrl", ['$scope', '$http', function ($scope, $http) {          
  $scope.variableToBeAssigned= null;
  $scope.formsubmit = function () {
         $http.post($scope.url, { "name": $scope.name}).
                    success(function (data, status){
                        $scope.variableToBeAssigned= "success";
                        return($scope.test1);
                    })}

}