问题描述
今天,我意识到Angular 5中反应形式的意外(对我而言)行为.服务器正在从应用程序接收一个值为"null"的字符串.而不是空值,这就是我想要的.
Today I realized about an unexpected (for me) behaviour of the reactive forms in Angular 5. The server was receiving from the app an string with the value "null" instead of the null value, whichh is what I wanted.
我做了以下测试:
https://stackblitz.com/edit/angular- rjrspr?file = app%2Fapp.component.html
如下图所示,如果我在选择项中使用[value]="null"
,则该字段将变为"null". (字符串文字)作为值.但是,如果使用[ngValue]="null"
,它将获得预期的null
值.
As you can see in the picture below, if I use [value]="null"
in the select, the field is getting "null" (string literal) as value. However, if I use [ngValue]="null"
it gets the expected null
value.
我认为使用value
用于反应形式,而ngValue
用于模板驱动形式.我想知道在我的表单中同时使用两种格式是否安全(我总是使用反应式表单),以及对于不同行为是否有任何解释.
I thought that using value
was intended for reactive forms, while ngValue
was for template driven forms. I'd like to know if it's safe to use both in my forms (I always use reactive forms) and if there's any explanation for the different behaviour.
谢谢!
推荐答案
可以使用value
或ngValue
.
两者之间的唯一区别是value
始终是字符串,在ngValue
中可以传递对象
The only difference between two is that value
is always string, where in ngValue
you can pass object
这篇关于Angular 5中value和ngValue之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!