2017-02-19 59 views
0

我试图为“聊天渠道”实施search method,对于每个查询,它代表来自数据库的来自SQL table的兼容结果。每个结果可以通过独特的ID这也是来自SQl表。我不想显示ID's,所以我把它们放在hidden input中,对于每个结果使用ng-model。另外,对于每个结果,用户有可能将订阅到频道(订阅按钮)显示在这个结果中,所以为了正确保存订阅,我需要获得保存在hidden input中的ID的结果。如何获得使用AngularJS动态存储的隐藏输入值

这里是我的尝试:

<table class="table table-condensed"> 
    <tr class="separating_line" ng-repeat="x in result"> 
     <!-- ng-bind-html allows to bind a variable to the innerHTML part of the HTML element --> 
     <td> 
      <div class="col-md-8"> 
      <h4><b style="color: gray;">{{ x.Name }}</b></h4> 
      <br><p> {{ x.Description }}</p> 
      </div> 
      <div class="col-md-2"> 
      <small><label style="color: gray;">Subscribers </label>{{ x.Subscribersnum }}</label></small><br> 
      <input id="hiddenId" hidden="hide" ng-model="idval=x.ChanID" /> 
      <button ng-click="subscribechannel()" class="btn btn-primary">Subscribe</button> 
      </div> 
     </td> 
    </tr> 
</table> 

controller我通过以下方式发送到方便Servlet

$http.get("http://localhost:8080/ExampleServletv3/subscribeservlet",{ 
     params: { 
      subidval: $scope.idval 
     } 
}) 

但调试我在servlet获得的价值时是null

我该如何解决这个问题?有任何想法吗?

谢谢

+0

没有理由在Angular应用程序中使用隐藏字段:您从不提交表单。始终发送AJAX请求。只需将数据存储在控制器的变量中即可。顺便说一句,ng-model不期望一个赋值给变量的语句。阅读关于ng-model的docuentation(虽然,我再说一遍,在这里你不需要它)。 –

+0

但我需要特定的结果值,我该怎么做? – user6561572

回答

2

你不需要任何隐藏的字段来做你想做的。您只需将参数传递给您的函数:

<tr class="separating_line" ng-repeat="x in result"> 
    ... 
    <button ng-click="subscribechannel(x)" class="btn btn-primary">Subscribe</button> 
+0

这很完美,很简单,非常感谢! – user6561572