什么是ng-model和ng-value之间的区别 [英] whats the difference between ng-model and ng-value

查看:321
本文介绍了什么是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模型。

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.

在单选按钮的上下文中,它允许您使用非字符串值。例如,如果您有单选按钮'是'和'否'(或等效),其值为'true'和'false' - 如果使用'value',存储在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值仍将始终被处理作为一个字符串。要为select设置非字符串值,请使用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天全站免登陆