【问题标题】:Angularjs: dynamically populated option select returns object instead of valueAngularjs:动态填充的选项选择返回对象而不是值
【发布时间】:2017-03-15 14:48:46
【问题描述】:

我正在尝试使用从数据库中获取的值动态填充 AngularJS 中 <select> HTML 元素的 options。根据文档,我试过这个:

<select 
    name="student_id" 
    ng-model="studentData.student_id"
    ng-options="student as student.name for student in students track by student.id"  
    class="form-control"> 
    <option value="">-- choose Agency --</option>
</select>

这很好用。它填充了optionselect HTML 元素,我可以看到填充了学生姓名的下拉值,option 元素的对应值是student_id

现在的问题是,当我填写所有表单并尝试提交时,它提交了一个 student object 而不是 student_id 作为@987654328 的值@ 元素。


如何提交student_id 而不是学生对象

【问题讨论】:

    标签: html angularjs forms


    【解决方案1】:

    在您的代码中,您选择整个 student 对象。改变这一行

    ng-options="student as student.name for student in students track by student.id"  
    

    收件人:

    ng-options="student.id as student.name for student in students track by student.id"  
    

    注意:我认为你也应该删除track by,我想它不会在这里带来任何东西。

    【讨论】:

      猜你喜欢
      • 2017-03-31
      • 1970-01-01
      • 1970-01-01
      • 2016-10-16
      • 1970-01-01
      • 2011-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多