ng-model 和 ng-value 之间有什么区别/不兼容? [英] What's the difference/incompatibility between ng-model and ng-value?

查看:38
本文介绍了ng-model 和 ng-value 之间有什么区别/不兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,ng-model 为分配了模型的特定元素设置了值.鉴于 ng-value 与 ng-model 有何不同?

As far as i understood ng-model sets the value for that particular element in which the model is been assigned. given that how is ng-value different from ng-model?

推荐答案

与 ng-model 配合使用;对于收音机和选择,它是在选择该项目时设置为 ng-model 的值.将其用作元素的 'value' 属性的替代,该属性将始终将字符串值存储到关联的 ng-model.

It works in conjunction with ng-model; for radios and selects, it is the value that is set to the ng-model when that item is selected. Use it as an alternative to the 'value' attribute of the element, which will always store a string value to the associated ng-model.

在单选按钮的上下文中,它允许您使用非字符串值.例如,如果您的单选按钮是"和否"(或等效项)的值为真"和假"——如果您使用值",则存储在 ng-model 中的值将成为字符串.如果您使用 'ng-value',它们将保持为布尔值.

In the context of radio buttons, it allows you to use non-string values. For instance, if you have the radio buttons 'Yes' and 'No' (or equivalent) with values 'true' and 'false' - if you use 'value', the values stored into your ng-model will become strings. If you use 'ng-value', they will remain booleans.

但是,在 select 元素的上下文中,请注意 ng-value 仍将始终被视为字符串.要为选择设置非字符串值,请使用 ngOptions.

In the context of a select element, however, note that the ng-value will still always be treated as a string. To set non-string values for a select, use ngOptions.

这篇关于ng-model 和 ng-value 之间有什么区别/不兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆