简体   繁体   English

当值没有改变时如何强制 ngModel 更新?

[英]How to force ngModel update when value did not change?

I want to limit number input to 0-100 range, but on input, not during validation.我想将数字输入限制在 0-100 范围内,但在输入时,而不是在验证期间。 I'm using ngModel to bind value and emitt change event:我正在使用 ngModel 绑定值并发出更改事件:

<input [ngModel]="value" (ngModelChange)="validate($event)" />

And then check if value exceeds given limits:然后检查值是否超过给定限制:

public validate(value) {
    if (value > 100)
        this.value = 100;
    if (value < 0)
        this.value = 0;
}

And this partialy works.这部分有效。 However if I say try to input 150 and value will switch to 100, I can then input anything over 100, because model value remains 100, and so input value is not updated.但是,如果我说尝试输入 150 并且值将切换为 100,那么我可以输入超过 100 的任何值,因为 model 值保持为 100,因此输入值不会更新。 Is there any way to manually force this update?有什么办法可以手动强制更新?

EDIT: I missed quite important bit here.编辑:我在这里错过了很重要的一点。 This behaviour seems to only occur to input with type=number.这种行为似乎只发生在输入 type=number 时。 Text input wont exceed 100. My workaround is as Faisal suggested using keypress event with preventDefault like so:文本输入不会超过 100。我的解决方法是 Faisal 建议使用带有 preventDefault 的按键事件,如下所示:

public keyPress(event) {
    let inputChar: number = +String.fromCharCode(event.charCode);
    if (this.value + inputChar > 100 || this.value + inputChar < 0) {
        // Exceeded limits, prevent input
        event.preventDefault();
    }
}

I had the same problem and found a different solution that I liked more.我遇到了同样的问题,并找到了我更喜欢的不同解决方案。 As you said the problem is that the value in the model is the same before and after your function. What I did was to call Angular's change detection before I change the value so it registers a change.正如您所说,问题是 model 中的值在您的 function 之前和之后是相同的。我所做的是在更改值之前调用 Angular 的更改检测,以便它注册更改。 For this use the ChangeDetectorRef class and call its detectChanges method.为此,请使用 ChangeDetectorRef class 并调用其 detectChanges 方法。

Thus, your function becomes:因此,您的 function 变为:

public validate(value) {
    this.changeDetector.detectChanges();
    if (value > 100)
        this.value = 100;
    if (value < 0)
        this.value = 0;
}

And it works perfectly.它完美地工作。 Hope it helps.希望能帮助到你。

Use a regular expression to limit the user input.使用正则表达式来限制用户输入。

Here is the html code for your input:这是您输入的 html 代码:

<input [(ngModel)]="value" 
       (keypress)="keyPress($event)"
       (ngModelChange)="validate($event)"  
        maxlength=3 />

.. and typescript code: ..和 typescript 代码:

keyPress(event: any) {
    const pattern = /[0-9]/;
    let inputChar = String.fromCharCode(event.charCode);

    if (!pattern.test(inputChar)) {
        // invalid character, prevent input
        event.preventDefault();
    }
}

validate(value:number) {
    if(value>100) {
        this.value=100;
    }
}

Here is a working plunker: Plunker DEMO这是一个工作的plunker: Plunker DEMO

I had a similar problem.我有一个类似的问题。 My input does not update, when the result equals to the previous entry value.当结果等于先前的输入值时,我的输入不会更新。

I use @ViewChild like that: In your template:我这样使用@ViewChild :在你的模板中:

<input #input [ngModel]="value" (ngModelChange)="validate($event)" />

In your TS file:在你的 TS 文件中:

@ViewChild('input') input: ElementRef;

public validate(value) {
    if (value > 100)
        this.value = 100;
    if (value < 0)
        this.value = 0;
    this.input.writeValue(this.value); // force to update
}

another way.. as i Know is to use NgZone (equivalent of AngularJs $scope.$apply() ).. like:另一种方式..据我所知是使用 NgZone(相当于 AngularJs $scope.$apply() )..喜欢:

constructor(private _ngZone:NgZone){

}


public validate(value){
    if(value>100)
        this.value=100;
    if(value<0)
        this.value=0;

this._ngzone.run(()=>{
 this.value=(value>100) ? 100  ? (value<0) ? 0 : 0;
});

}

You can add else part to reflect changes.您可以添加其他部分以反映更改。

public validate(value){
    if(value>100)
        this.value=100;
    else
        this.value = value    
}

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

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