angular $http post return 成功

angular $http post return on success

大家好,由于我的一个项目出现了一些情况,我有以下情况。 我正在使用 angular-xeditable,它有一个 onbefore save 方法,它应该 returns 一个字符串,以防我希望 from 保持打开(可编辑)和 true 以防我想形成已关闭(不可编辑)。

现在的问题,你会在下面找到我的一个angular函数的代码

self.validateBeforeSave = function(data, id){
          var current_id = id;
          $http.post('data/functions.php', {
                action : 'updateQuotasDisease',
                sqlPeriod : data.period,
                sqlDiseaseCode : data.disease_code,
                sqlTargetCountry : data.target_country,
                sqlTargetSpecialty : data.target_specialty,
                sqlChartsAmount : data.charts_amount,
                sqlAmount : data.amount,
                sqlStatus : data.status
              })
              .success(function(response) {
                if(response == 'quota-exists'){
                  $("#"+current_id).css("background-color", "#ffc4c4");
                  swal("That quota already exists!", "", "error");
                  return "error-msg";
                }
                else{
                  $("#"+current_id).css("background-color", "#ffffff");
                  return true;
                }
              })
              .error(function(response) {
                console.log('Error: ' + response);
            });


        };

此代码是从 HTML 调用的,但基本上重要的是需要 return 来自以前的函数 true 或 "string",你可以找到 onbeforesave="$ctrl.validateBeforeSave($data, line.id)" 从那里我调用了上一个函数。

<table class="table general-tables table-responsive" ng-show="$ctrl.VisibleQuotasDisease">
                <thead>
                    <tr>
                        <th>Actions</th>
                        <th>Period</th>
                        <th>Disease code</th>
                        <th>Target country</th>
                        <th>Target specialty</th>
                        <th>Charts amount</th>
                        <th>Amount</th>
                        <th>Status</th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="line in $ctrl.quotasDisease" id="{{line.id}}">
                        <td style="white-space: nowrap">
                            <!-- onaftersave="$ctrl.saveRowDisease($data, line.id, line) validateBeforeSave"  -->
                            <form editable-form name="rowform" onbeforesave="$ctrl.validateBeforeSave($data, line.id)" ng-show="rowform.$visible" class="form-buttons form-inline" shown="inserted == line">
                                <button type="submit" ng-disabled="rowform.$waiting" class="btn btn-xs btn-primary">
                                    <i class="fa fa-2x fa-save"></i>
                                </button>
                                <button type="button" ng-disabled="rowform.$waiting" ng-click="rowform.$cancel()" class="btn btn-xs btn-default">
                                    <i class="fa fa-2x fa-close"></i>
                                </button>
                            </form>
                            <div class="buttons" ng-show="!rowform.$visible">
                                <button class="btn btn-xs btn-warning" ng-click="rowform.$show()">
                                    <i class="fa fa-2x fa-edit"></i>
                                </button>
                                <button class="btn btn-xs btn-danger" ng-click="$ctrl.removeRowDisease($index, line)">
                                    <i class="fa fa-2x fa-trash-o"></i>
                                </button>
                            </div>  
                        </td>
                        <td>
                            <span editable-text="line.period" e-class="period-inputs" e-name="period" e-form="rowform" e-maxlength="7" e-required>
                                {{line.period}}
                            </span>
                        </td>
                        <td>
                            <span editable-text="line.disease_code" e-name="disease_code" e-form="rowform" e-maxlength="2" e-required>
                                {{line.disease_code}}
                            </span>
                        </td>
                        <td>
                            <span editable-text="line.target_country" e-name="target_country" e-form="rowform" e-maxlength="2" e-required>
                                {{line.target_country}}
                            </span>
                        </td>
                        <td>
                            <span editable-text="line.target_specialty" e-name="target_specialty" e-form="rowform" e-maxlength="4" e-required>
                                {{line.target_specialty}}
                            </span>    
                        </td>
                        <td>
                            <span editable-text="line.charts_amount" e-name="charts_amount" e-form="rowform"  e-onkeypress="return onlyInt(event)" e-required>
                                {{line.charts_amount}}
                            </span>
                        </td>
                        <td>
                            <span editable-text="line.amount" e-name="amount" e-form="rowform"  e-onkeypress="return onlyInt(event)" e-required>
                                {{line.amount}}
                            </span>
                        </td>
                        <td>
                            <span editable-text="line.status" e-name="status" e-form="rowform"  e-onkeypress="return onlyInt(event)" e-required>
                                {{line.status}}
                            </span>
                        </td>
                    </tr>
                </tbody>
            </table>

最后我想问一下如何从 $http post 的成功部分中执行 return 或如何解决这种情况。

提前致谢。

就像这里的另一段代码一样,我正在调用 php 函数

if($request -> action == 'updateQuotasDisease'){
        $period_sql = $request -> sqlPeriod; 
        $disease_code_sql = $request -> sqlDiseaseCode;
        $target_country_sql = $request -> sqlTargetCountry; 
        $target_specialty_sql = $request -> sqlTargetSpecialty;
        $charts_amount_sql = $request -> sqlChartsAmount; 
        $amount_sql = $request -> sqlAmount;
        $status_sql = $request -> sqlStatus;

        $existing_record = connDB() -> getOne("SELECT count(*) FROM quota_period WHERE period_field = '$period_sql' AND disease_code_numeric = '$disease_code_sql' AND targeted_country = '$target_country_sql' AND targeted_specialty_numeric_code = '$target_specialty_sql' AND amount = $amount_sql AND patient_cases_amount = $charts_amount_sql AND status = $status_sql;");
        if($existing_record < 1){
            connDB() -> query("UPDATE quota_period SET period_field = '$period_sql', disease_code_numeric = '$disease_code_sql', targeted_country = '$target_country_sql', targeted_specialty_numeric_code = '$target_specialty_sql', patient_cases_amount = $charts_amount_sql, amount = $amount_sql, status = $status_sql WHERE period_field = '$period_sql' AND disease_code_numeric = '$disease_code_sql' AND targeted_country = '$target_country_sql' AND targeted_specialty_numeric_code = '$target_specialty_sql';");
        }
        else{
            echo "quota-exists";
        }
    }

首先,您的 return return 只为成功 call/caller。在成功之外是抓不到的。

第二个想法来自 ajax 电话。来自 angular 的 http post 被编写为始终异步。因此,您的函数永远不会等待 ajax 请求完成。

但是您可以使用 $q 模块来使函数将 "wait" 并将结果接收到 return 它。

是这样的:

function() {
    var deferred = $q.defer();

    $http.post('data/functions.php', arguments);
    .success(function(response) {
        //your code

        //resolve the promise
        deferred.resolve('request successful');
    })
    .error(function(data,status,headers,config){
        //reject the promise
        deferred.reject('ERROR');
    });

    return deferred.promise;
}

你只能从deferred.promise(我不知道)中确保你想要的结果。

您可以在上面的这些链接中看到更多信息:

To then() or to success() in AngularJS