且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ngModel.$modelValue 和 ngModel.$viewValue 有什么区别

更新时间:2021-08-26 03:51:21

您正在查看正确的文档,但可能只是您有点困惑.$modelValue$viewValue 有一个明显的区别.是这样的:

You are looking at the correct documentation, but it might just be that you're a little confused. The $modelValue and $viewValue have one distinct difference. It is this:

如上所述:

$viewValue: 视图中的实际字符串(或对象) 值.
$modelValue: 模型中控件绑定的值.

$viewValue: Actual string (or Object) value in the view.
$modelValue: The value in the model, that the control is bound to.

我将假设您的 ngModel 指的是 <input/> 元素...?所以你的 有一个显示给用户的字符串值,对吗?但实际模型可能是该字符串的其他版本.例如,输入可能显示字符串 '200'(例如)实际上将包含一个模型值200 作为整数.因此,您在 中查看"的字符串表示是 ngModel.$viewValue 而数字表示将是 ngModel.$modelValue代码>.

I'm going to assume that your ngModel is referring to an <input /> element...? So your <input> has a string value that it displays to the user, right? But the actual model might be some other version of that string. For example, the input might be showing the string '200' but the <input type="number"> (for example) will actually contain a model value of 200 as an integer. So the string representation that you "view" in the <input> is the ngModel.$viewValue and the numeric representation will be the ngModel.$modelValue.

另一个示例是 <input type="date">,其中 $viewValue 类似于 Jan 01, 2000> 和 $modelValue 将是一个实际的 javascript Date 对象,表示该日期字符串.有意义吗?

Another example would be a <input type="date"> where the $viewValue would be something like Jan 01, 2000 and the $modelValue would be an actual javascript Date object that represents that date string. Does that make sense?

希望能回答您的问题.