流量:如何在Angular 2中使用ngModel過濾空值?
- 用戶提交的一些數據的形式,一些可選字段用戶沒有填寫保存在蒙戈爲「零」。
- 稍後,用戶決定編輯該表單。
- 服務抓取表單並將其顯示給用戶。
- 表單輸入框顯示'null',因爲在步驟1中他沒有填充它們。
所以,我創建了一個管道:
import {Pipe, PipeTransform} from '@angular/core';
@Pipe({
name: 'transformNull'
})
export class TransformNull implements PipeTransform {
transform(value) {
if (value == null) {
return null;
}
}
然後,在HTML我試圖打破ngModel:
[ngModel]="user.extraInfo | transformNull" (ngModelChange)="user.extraInfo.value=$event"
這引起了這個錯誤:
Cannot set property 'value' of undefined TypeError: Cannot set property 'value' of undefined
我試圖用* ngIf =「isDataAvailable」推遲頁面加載,錯誤消失,用戶可以保存表格,該值存儲到mongo ,但刷新頁面時,輸入框爲空。
然後,我刪除了* ngIf,添加了elvis操作符,但值錯誤又回來了。
我使用zone.js v0.7.4,嘗試0.7.2和0.7.5,但沒有運氣。
我在這裏錯過了什麼?
你檢查https://stackoverflow.com/questions/36016407/two-way-binding-with-elvis-operator/36016472#comment70500052_36016472? – echonax
怎麼樣:(ngModelChange)=「user.extraInfo = $ event」並且請刪除你的管道「transformNull」 –
@SaeedPy刪除管道,改變你提到的部分,在輸入框中仍爲空。 –