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

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

问题描述

今天我意识到 Angular 5 中响应式表单的意外(对我而言)行为.服务器从应用程序接收到一个值为null"的字符串;而不是空值,这是我想要的.

我做了以下测试:

我认为使用 value 是用于响应式表单,而 ngValue 用于模板驱动的表单.我想知道在我的表单中使用两者是否安全(我总是使用反应式表单),以及是否对不同的行为有任何解释.

谢谢!

解决方案

使用 valuengValue 都可以.

两者唯一的区别是value总是字符串,在ngValue中你可以传递对象

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.

I did the following test:

https://stackblitz.com/edit/angular-rjrspr?file=app%2Fapp.component.html

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.

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.

Thanks!

解决方案

Its ok to use valueor 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天全站免登陆