Javascript 在更好的实践中保存和加载表单值
我在页面上有下拉选择 代码看起来像Javascript 在更好的实践中保存和加载表单值,javascript,angularjs,Javascript,Angularjs,我在页面上有下拉选择 代码看起来像 <td> <ui-select ng-model="user_profile.gender" required="true"> <ui-select-match > {{$select.selected.name}} </ui-select-match> <ui-select-choices repeat="option in selectOptions.gender | props
<td>
<ui-select ng-model="user_profile.gender" required="true">
<ui-select-match > {{$select.selected.name}} </ui-select-match>
<ui-select-choices repeat="option in selectOptions.gender | propsFilter: {name: $select.search} ">
<div ng-bind-html="option.name | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
</td>
但是,数据库中字段上的值是整数类型
女性0,男性1
如果没有太多硬代码过程,如何将性别值从数据库加载到页面
因为我能想出这样一个愚蠢的方法
用于加载页面
- 从API加载性别值
- 设置性别下拉列表的选定值
- 提取性别下拉列表的选定值
- 向API发送PUT请求以保存更改
谢谢如果您唯一关心的是在重复ui选择时匹配Id值,那么您只需要更改:
<ui-select-choices repeat="option in selectOptions.gender | propsFilter: {name: $select.search} ">
致:
原因是,您当前只是说我想使用整个模型作为所选值。通过声明“options.id as option”表示我将为您提供整个对象以在重复中使用,但当选择某个对象时,我会将该id设置为所选值,并将其用作设置值的比较
快速示例位于如果您唯一关心的是在重复ui选择时匹配Id值,那么您只需要更改:
<ui-select-choices repeat="option in selectOptions.gender | propsFilter: {name: $select.search} ">
致:
原因是,您当前只是说我想使用整个模型作为所选值。通过声明“options.id as option”表示我将为您提供整个对象以在重复中使用,但当选择某个对象时,我会将该id设置为所选值,并将其用作设置值的比较
快速的例子是可能我也很笨,我想和你一样:-)我认为类似的工作流程是必须的,但是有没有方便的库可以帮助我们编写更少的代码。例如
ngResource
节省大量时间编写大量代码,以便与web服务器进行协商:(可能我也很愚蠢,我的想法与您的想法相同:-)我认为类似的工作流程是必须的,但是否有任何方便的库可以帮助我们编写更少的代码。例如ngResource
节省大量时间编写大量代码,以便与web服务器进行协商:(
<ui-select-choices repeat="option.id as option in selectOptions.gender | propsFilter: {name: $select.search} ">