角度的奇怪行为需要TextBox

时间:2022-05-03 17:23:59

I have 2 textboxes, one which is required and the other one which is not required.

我有2个文本框,一个是必需的,另一个是不需要的。

If we add text in the required text box, say "ABC", then remove the content, the ng-model is set to undefined

如果我们在所需的文本框中添加文本,例如“ABC”,然后删除内容,则ng-model设置为undefined

If we add text to the non required field and remove the content, the ng-model is not to empty string "".

如果我们将文本添加到非必填字段并删除内容,则ng-model不会清空字符串“”。

Here is a plunk about the behavior I've explained above. Please use the console to look at the result.

以下是我上面解释过的行为。请使用控制台查看结果。

http://plnkr.co/edit/XgQBfcyRF3OwG1qC0gXb?p=preview

http://plnkr.co/edit/XgQBfcyRF3OwG1qC0gXb?p=preview

Why is there a difference in setting the ng-model between the two?

为什么在两者之间设置ng模型有区别?

1 个解决方案

#1


2  

It is seems to be by design, and has to do with a consistent behavior of validated form values.

它似乎是设计的,并且与验证的表单值的一致行为有关。

There isn't anything on this behaviour in the docs, AFAIK.. It is implied here -> https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L1045

文档中没有任何关于此行为的内容,AFAIK ..这里隐含着 - > https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L1045

With angularJS validation, if a value entered on field is allowed, that becomes the model's value (as any other bound value would behave). If it isn't, the only way to be consistent is to set the value as undefined (as no allowed value is in the field). The two other options would be to keep it's last valid value, or keep binding the wrong value, only triggering the flags for invalid field, and form. Both these solutions are bad - leaving the last value is probably not wanted (if you were to use the values disregarding the state of the form, it would lead to errors) and allowing for invalid values is a terrible sin ;) (you couldn't trust the validation service to help prevent buggy use of wrong types)

使用angularJS验证,如果允许在字段上输入的值,则该值将成为模型的值(与任何其他绑定值的行为一样)。如果不是,则唯一的一致方法是将值设置为undefined(因为字段中没有允许的值)。另外两个选项是保持它的最后一个有效值,或者保持绑定错误的值,只触发无效字段和表单的标志。这两个解决方案都很糟糕 - 留下最后一个值可能是不想要的(如果你使用的值忽略了表单的状态,它会导致错误)并且允许无效值是一个可怕的罪恶;)(你不能' t信任验证服务,以帮助防止错误使用错误的类型)

Although it may seem strange or even inconsistent, it really isn't. I have slightly modified your plunkr to validate for a number, which I believe makes it clearer why it is this way: http://plnkr.co/edit/9gJmblUn9MUUeFt5lWJZ?p=preview.

虽然它看似奇怪甚至不一致,但实际上并非如此。我稍微修改了你的plunkr以验证一个数字,我相信它更清楚为什么这样:http://plnkr.co/edit/9gJmblUn9MUUeFt5lWJZ?p = preview。

So, in reality there is no difference - only in your second input an empty string is considered a valid, thus accepted value for that field.

所以,实际上没有区别 - 只有在你的第二个输入中,空字符串被认为是有效的,因此该字段的接受值。

#1


2  

It is seems to be by design, and has to do with a consistent behavior of validated form values.

它似乎是设计的,并且与验证的表单值的一致行为有关。

There isn't anything on this behaviour in the docs, AFAIK.. It is implied here -> https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L1045

文档中没有任何关于此行为的内容,AFAIK ..这里隐含着 - > https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L1045

With angularJS validation, if a value entered on field is allowed, that becomes the model's value (as any other bound value would behave). If it isn't, the only way to be consistent is to set the value as undefined (as no allowed value is in the field). The two other options would be to keep it's last valid value, or keep binding the wrong value, only triggering the flags for invalid field, and form. Both these solutions are bad - leaving the last value is probably not wanted (if you were to use the values disregarding the state of the form, it would lead to errors) and allowing for invalid values is a terrible sin ;) (you couldn't trust the validation service to help prevent buggy use of wrong types)

使用angularJS验证,如果允许在字段上输入的值,则该值将成为模型的值(与任何其他绑定值的行为一样)。如果不是,则唯一的一致方法是将值设置为undefined(因为字段中没有允许的值)。另外两个选项是保持它的最后一个有效值,或者保持绑定错误的值,只触发无效字段和表单的标志。这两个解决方案都很糟糕 - 留下最后一个值可能是不想要的(如果你使用的值忽略了表单的状态,它会导致错误)并且允许无效值是一个可怕的罪恶;)(你不能' t信任验证服务,以帮助防止错误使用错误的类型)

Although it may seem strange or even inconsistent, it really isn't. I have slightly modified your plunkr to validate for a number, which I believe makes it clearer why it is this way: http://plnkr.co/edit/9gJmblUn9MUUeFt5lWJZ?p=preview.

虽然它看似奇怪甚至不一致,但实际上并非如此。我稍微修改了你的plunkr以验证一个数字,我相信它更清楚为什么这样:http://plnkr.co/edit/9gJmblUn9MUUeFt5lWJZ?p = preview。

So, in reality there is no difference - only in your second input an empty string is considered a valid, thus accepted value for that field.

所以,实际上没有区别 - 只有在你的第二个输入中,空字符串被认为是有效的,因此该字段的接受值。