Angular 5中value和ngValue之间的差异 [英] Differences between value and ngValue in Angular 5

查看:105
本文介绍了Angular 5中value和ngValue之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我意识到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.

谢谢!

推荐答案

可以使用valuengValue.

两者之间的唯一区别是value始终是字符串,在ngValue中可以传递对象

The only difference between two is that value is always string, where in ngValue you can pass object

这篇关于Angular 5中value和ngValue之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆