简体   繁体   English

Angular2模型绑定不起作用

[英]Angular2 model binding not working

I try to update an input value in Angular 2, it works the first time the size value exceeds maxSize, but afterwords it does not work anymore. 我尝试在Angular 2中更新输入值,它在大小值第一次超过maxSize时起作用,但事后便不再起作用。 It seems like when I am setting this.size to some value the UI is not updated, am I overlooking something ? 似乎当我将this.size设置为某个值时,UI尚未更新,我是否忽略了某些内容?

HTML: HTML:

<input type="text" class="form-control" [value]="size" (input)="size = updateValue($event)">

Code: 码:

export class BrushSizePicker {
@Input() minValue;
@Input() maxValue;
@Input() size;

increaseValue(event) {
this.size++;

this.checkValue();
}

decreaseValue(event) {
this.size--;

this.checkValue();
}

updateValue(event) {
this.size = parseInt(event.target.value);
this.checkValue();

return this.size;
}

private checkValue() {
if (this.size > this.maxValue) {
  this.size = this.maxValue;
}
if (this.size < this.minValue) {
  this.size = this.minValue;
}

}

EDIT: I logged what happened: checkValue is called every time with the correct input, and it returns the correct value. 编辑:我记录了发生的情况:每次使用正确的输入调用checkValue,它返回正确的值。 But the new value is not set into the input field / value field 但是新值未设置到输入字段/值字段中

While it may not solve the problem, the way you have implemented the input event can be simplified. 尽管它可能无法解决问题,但是可以简化实现输入事件的方式。 I would have written it like this, side-effect free functions: 我会这样写,没有副作用的函数:

updateValue(event) {  // The method name with this change is a misnomer
   return this.checkValue(parseInt(event.target.value));
}

private checkValue(item) {
 if (item > this.maxValue) {
   return this.maxValue;
 }
 else if (else < this.minValue) {
   return this.maxValue;
 }
 return item;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM