3
我創建一個簡單的示例:plnkr。嘗試在第一個字段中輸入123qwe之類的內容。 「qwe」不會消失。但是如果輸入一個新數字,所有非數字字符將會消失。Angular2更改檢測問題。輸入字段不更新,如果類字段does'n更改,但輸入字段值不同
在第二個字段中,非數字字符替換爲'!'並且該領域按預期工作。
我的代碼:
@Component({
selector: 'my-app',
providers: [],
template: `
<div>
Doesn't work: <input [value]="val | replace:pattern:''" (input)="val=$event.target.value">
<br>
Works: <input [value]="val2 | replace:pattern:'!'" (input)="val2=$event.target.value">
</div>
`,
directives: []
})
export class App {
val: string = "";
val2: string = "";
pattern = /[^0-9]/g;
constructor() {
}
}
有任何解決方法,使第一場的工作像一個第二,但與空字符串替換?有沒有預期的行爲?