2016-11-29 36 views
0

因此,我們假設我將此表單發佈到某個Web服務。從承諾數據設置表單輸入值

<form name="myForm" id="myForm" method="post" action="https://servicelink.....etc" accept-charset="utf-8"> 
      <input type="hidden" name="accepturl" value="{{$ctrl.accepturl}}" ng-model="$ctrl.accepturl"> 
      <input type="hidden" name="callbackurl" value="{{$ctrl.callbackurl}}" ng-model="$ctrl.callbackurl"> 
      <input type="hidden" name="orderid" value="{{$ctrl.orderid}}" ng-model="$ctrl.orderid"> 
     </form> 

這裏是我的角度控制器:(我使用AngularJS 1.5.8與組件)

function controller(someservice) { 
    var ctrl = this; 
    ctrl.accepturl = $window.location.href; 
    ctrl.callbackurl = "https://........"; 

    ctrl.placeOrder = function() { 
     someservice.placeOrder(ctrl.amount).then(onOrderInitiation, onOrderInitiationError); 
    }; //$http.post call is made there 

    function onOrderInitiation(data) { 
     ctrl.orderid = data; 
     // I tried to set the input value by javascript as well with no success 
     // document.getElementsByName('orderid').value = data; 

     document.getElementById("flexwinSubmit").submit(); 
    } 
} 

這裏是$ http.post完成服務:

this.placeOrder = function(amount){ 
      return $http.post("/rest/.........) 
       .then(function(response){ 
       return response.data.orderId; 
      }); 
     }; 

所以我必須更新orderid輸入值在我的HTML從承諾函數數據(onOrderInitiation)然後提交表單。但我無法通過任何方式更新它。然而ctrl.orderid設置正確(我驗證了在日誌)

當我試圖把它的JavaScript的方式,該值正確更改 - 當我CONSOLE.LOG它。但是在提交表單時,值不會被設置。

document.getElementsByName('orderid').value = data; 
+0

可能重複[如何從異步調用返回響應?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an -asynchronous呼叫) – baao

+0

您必須使用NG-價值不僅僅是值,即NG-值=「ctrl.orderid」 – GraveyardQueen

+0

@GraveyardQueen它沒有任何區別:(有趣的事情時,我檢查它在瀏覽器等等在提交表單時設置了值,但是沒有設置!! – Hazu

回答

0

所以我通過在提交表單之前設置$ timeout來完成它。

$timeout(function() { 
       document.getElementById("myForm").submit(); 
      }, 200);