2016-02-08 33 views
0

我有我的模型變量已經綁定到窗體中的一些隱藏的輸入字段。如何在AngularJS的submssion之前更新表單隱藏數據?

當用戶點擊提交按鈕時,我嘗試通過更新相應的綁定模型變量來更新這些字段,但它們並沒有改變(我注意到當將請求轉儲到服務器上時,我總是收到舊數據),我也注意到,當我使用普通的文本輸入,而不是隱藏投入的每一件事工程確定

這裏是我的代碼:

形式

<form name="bla bla" action="bla bla" method="post" ng-submit="updateForm()"> 
    <input type="hidden" name="token" ng-model= "extra.token" /> 
    <input type="hidden" name="filters" ng-model="extra.filters" /> 
    <button type="submit"> 
    submit form 
    </button> 
</form> 

控制器

var app = angular.module(... // bla bla 

app.controller('MyController', ['$scope', ctrlDef]); 

function ctrlDef($scope) { 
    $scope.extra = {}; 
    $scope.extra.token = ''; 
    $scope.extra.filters = ''; 

    $scope.updateForm = function() { 
     $scope.extra.token = 'test1'; 
     $scope.extra.filters = 'test2'; 
    }; 
} 
+0

要重新加載表單上的屏幕提交嗎?因爲如果您使用帶有操作屬性的表單,它將重新加載頁面。這是要求還是可以開啓POST XHR呼叫? –

+0

@ShripalSoni不,實際上表單提交給一個隱藏的iframe,它依次使用隱藏的數據生成可下載的文件 –

+1

而不是type =「hidden」您是否嘗試過使用ng-hide =「true」和type =「text 「 – Harbinger

回答

1

我不認爲ngSubmit將可靠的POST請求之前,如果你使用標準的HTML表單屬性來運行(methodaction等)。更好的主意是使用$html service發送您的請求。這也是異步的好處,所以你不應該像現在這樣需要一個隱藏的iframe。

app.controller('MyController', ['$scope', '$html', ctrlDef]); 

function ctrlDef($scope, $html) { 
    $scope.extra = {}; 
    $scope.extra.token = ''; 
    $scope.extra.filters = ''; 

    $scope.updateForm = function() { 
     $scope.extra.token = 'test1'; 
     $scope.extra.filters = 'test2'; 

     $http.post(extra, "your post url here").then(function (response) { 
      // do stuff with the response here... 
     }); 
    }; 
} 
+0

我不想使用XHR調用 –

+0

定期執行「action」是因爲表單提交給隱藏的iframe用於在同一頁面下載文件 –

+0

我看不到這怎麼可能用XHR實現同樣簡單 - 說,我不知道你的代碼庫,所以我可能是錯的! –

相關問題