【发布时间】:2016-06-19 14:02:45
【问题描述】:
考虑以下组件
import {Component} from 'angular2/core'
@Component({
selector: 'my-app',
providers: [],
template: `
<div>
<h3>Input with two decimals</h3>
<input type="text"
[value]="formattedN"
(change)="nChanged($event.target.value)"/>
</div>
`,
directives: []
})
export class App {
private n: number = 0;
private formattedN: string = "0.00"
public nChanged(value) {
this.n = parseFloat(value);
this.formattedN = this.n.toFixed(2);
}
}
输入应始终是带有两位小数的数字。然而,情况并非总是如此。尝试删除最后一个零,该字段不会更改为我想要的 0.00。我知道它不起作用,因为 formattedN 值没有更新,这意味着属性绑定没有更新,因此输入的值没有更新。
在 plunker 中运行示例:http://plnkr.co/edit/Vyi4RKladslrdZslZQhm
有没有人能很好地解决这个问题?
【问题讨论】:
-
@GünterZöchbauer 不认为这是相关的,因为我的输入是文本输入。
-
@SimonHawesome 并没有真正帮助我为输入提供我想要的行为。
标签: javascript data-binding angular angular2-template angular2-forms