我想传递一个变量的值,如果它为空,则在 Angular 中调度它时自动将其值设置为 0
this.myForm = this.formBuilder.group({
id: [null, Validators.required],
productPrice: [0],
finalPrice: [0],
remarks: [null],
});
onCreate(form: FormGroup) {
console.log(form.value)
this.store.dispatch(form.value)
}
答案 0 :(得分:0)
如果左手为空,您可以使用空合并运算符返回参数的右手
const a = null;
const b = 5;
console.log( a ?? 0 ); // returns 0
console.log( b ?? 0 ); // returns 5
如果您确实想在求值后设置左侧,也可以将此运算符与赋值 ??=
一起使用。
答案 1 :(得分:0)
一种方法是使用 Array#reduce
和 Object.keys()
函数手动检查值,或者 ||
在值为 0
或 { 时使用 null
{1}}。
试试下面的方法
undefined