且构网

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

什么是ngModel。$ modelValue和ngModel。$ viewValue之间的差异

更新时间:2022-06-14 22:09:52

您正在寻找正确的文档,但它可能仅仅是因为你是一个有点困惑。在 $ 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 value in the view.
$modelValue: The value in the model, that the control is bound to.

我要假设你ngModel是指一个<输入/> 元素......?所以,你的<输入> 有它显示给用户,右一个字符串值?但实际的模型可能是字符串的其他版本。例如,输入可能会显示字符串'200'<输入类型=数字> (例如)实际上将包含 200 作为一个整数模型值。因此字符串重新presentation您在视图<输入方式> ngModel $ viewValue 和数字重新presentation将是 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.

另一个例子是&LT;输入类型=日期&GT; 其中 $ viewValue 会像 2000年1月1日 $ modelValue 将是一个实际的JavaScript 日期对象重新presents的日期字符串。这是否有意义?

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?

我希望回答你的问题。