简体   繁体   English

从使用ng-repeat的表单发布角度JS数据

[英]angular JS posting data from a form that uses ng-repeat

I am trying to post data from a html form that utilizes ng-repeat to generate several input text elements and associated dropdown menus. 我正在尝试从利用ng-repeat生成多个输入文本元素和关联的下拉菜单的html表单中发布数据。 My html template is below, and I am wondering if there is anyway to post the data in "items" or some other ng-model naming method as a collection to the controller. 我的html模板在下面,我想知道是否有任何数据可以将数据发布到“项目”或某些其他ng-model命名方法中,作为控制器的集合。

 <form ng-submit="submitAssignments()">
    <input type="submit" value="Submit" />
        <div ng-repeat="items in taskList">
               <input type="text" ng-model="items.task" readonly="readonly"  />
               <select ng-model="items.definition" 
                ng-options="option.id as option.definition for option in taskListGroup"></select>
        </div>

I created a jsfiddle( http://jsfiddle.net/y4cQD/24/ ) with the what I am trying to do, leaving one of the functions empty as I have yet to figure out how to pass the elements from ng-repeat to a post function. 我使用要执行的操作创建了一个jsfiddle( http://jsfiddle.net/y4cQD/24/ ),其中一个功能为空,因为我还没有弄清楚如何将元素从ng-repeat传递给岗位职能。

I have been reading up on how ng-repeat creates its own scope, and how it acts a bit odd when you attach the ng-model to primitives (which I think I avoided), but nonetheless I am bit stumped as to where to go from here. 我一直在阅读ng-repeat如何创建自己的作用域,以及将ng-model附加到基元时它如何工作(我认为我避免了),但是尽管如此,我仍然迷失了去向从这里。

If the strategy in my jsFiddle is not possible to pull off in angular, another option I was thinking of is using ng-repeat to create entirely new forms instead of just new elements and maybe set up a watcher to just post the data per form when the user changes the dropdown. 如果我的jsFiddle中的策略无法按角度进行,那么我想到的另一种选择是使用ng-repeat来创建全新的表单,而不仅仅是新元素,并且可能设置了一个观察者,以便在用户更改下拉列表。 Anyway, as I have been stuck on this for a bit longer than I would like to admit, so any help would be greatly appreciated. 无论如何,由于我在这个问题上停留的时间比我想承认的要长一些,因此我们将不胜感激。 Thank you in advance. 先感谢您。

Your fiddle seems to work fine. 你的小提琴似乎工作正常。 If you just output the taskList somewhere in the fiddle using {{taskList}} and play with the dropdown, you can see that the selected dropdown value is getting updated in the taskList item. 如果仅使用{{taskList}}在小提琴中的某个位置输出taskList并使用下拉菜单,则可以在taskList项目中看到所选的下拉列表值正在更新。

You can now use this task list to post to the server using $http or $resource . 现在,您可以使用此任务列表通过$http$resource发布到服务器。 See here 这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM