2016-09-21 44 views
0

我正在開發Angularjs應用程序,我有一個要求,即必須將隱藏變量發送給第三方應用程序。在angularjs中動態創建隱藏字段

這些隱藏變量的值應該來自數據庫。

我正在使用以下代碼動態創建隱藏變量。當用戶點擊提交按鈕

$scope.getDetailsForTP = function() { 
     $scope.models.MyModel.templateVariables = {}; 
     $http({ 
      url: "http://localhost:11149/MyService.svc/TemplateVariable", 
      dataType: "json", 
      headers: { 
       'Content-Type': 'application/json; charset=utf-8' 
      } 
     }).then(function successCallback(response) { 
      if (response.status == 200) { 
       $scope.models.MyModel.templateVariables = response.data; 
       $scope.submitForm(); 
      } 
      else { 
       alert('Error occurred in fetching template variable data'); 
      } 
     }, function errorCallback(response) { 
      //do something 
     }); 
    }; 

    $scope.submitForm = function() { 
     document.getElementById("apirequest").submit(); 
    }; 

隱變量在頁面上正確地呈現,但是當我檢查提琴手我找不到提交的隱藏變量

<input type="hidden" ng-repeat="hdnvar in models.MyModel.templateVariables" name="{{hdnvar.Key}}" id="{{hdnvar.Key}}" value="{{hdnvar.Value}}" /> 

Follwing函數被調用。

有人可以幫忙嗎?

+1

你爲什麼要提交你的表格。您可以使用$ http服務發佈您的數據,並在請求的數據字段中傳遞您的模態數據(數組)。 –

+0

感謝您的評論。你能否提供一個例子來做到這一點? –

回答

1

您在更新templateVariables後立即提交表單。它需要一些時間來呈現html元素。

所以你需要延遲一段時間後才能提交表格。

$scope.models.MyModel.templateVariables = response.data; 
$timeout($scope.submitForm, 1000) // submit form after 1 second 
+0

添加延遲將解決此問題。但是,呈現HTML元素可能需要一秒多的時間。有什麼方法可以在呈現HTML元素後才能提交表單? –