2016-09-21 64 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元素后才能提交表单? –