-1

大家好,由于我的一个项目的某些情况,我有以下情况。我正在使用 angular-xeditable,它有一个 onbefore save 方法,它应该返回一个字符串,以防我希望 from 保持打开(可编辑),如果我想关闭(不可编辑),则返回 true。

现在的问题是,您将在下面找到我的一个角度函数的代码

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 调用的,但基本上重要的是需要从以前的 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 的成功部分返回,或者如何解决这种情况。

提前致谢。

就像这里的另一段代码一样,我正在调用的 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";
        }
    }
4

1 回答 1

-2

首先,您的回报仅返回给成功呼叫者/呼叫者。它不能在成功之外被抓住。

第二个想法来自ajax调用。来自 Angular 的 http 帖子被写成总是异步的。因此,您的函数将永远不会等待 ajax 请求完成。

但是您可以使用该$q模块使函数“等待”并接收结果以返回它。

是这样的:

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(我不知道)。

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

AngularJS 中的 then() 或 success()

如何使用 AngularJS 进行 $http 同步调用

如何在 Angular js 中进行同步 http 请求

于 2017-03-13T20:01:04.797 回答