只有在输入焦点发生变化后才会调用change事件.我怎样才能使事件在每个按键上触发?
<input type="text" [(ngModel)]="mymodel" (change)="valuechange($event)" />
{{mymodel}}
Run Code Online (Sandbox Code Playgroud)
第二个绑定在每个按键btw上发生变化.
小智 385
我刚刚使用了事件输入,它的工作原理如下:
在.html文件中:
<input type="text" class="form-control" (input)="onSearchChange($event.target.value)">
Run Code Online (Sandbox Code Playgroud)
在.ts文件中:
onSearchChange(searchValue: string): void {
console.log(searchValue);
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*cok 180
使用ngModelChange通过打破了[(x)]语法到它的两片,即财产数据绑定和事件绑定:
<input type="text" [ngModel]="mymodel" (ngModelChange)="valuechange($event)" />
{{mymodel}}
Run Code Online (Sandbox Code Playgroud)
valuechange(newValue) {
mymodel = newValue;
console.log(newValue)
}
Run Code Online (Sandbox Code Playgroud)
它也适用于退格键.
Sag*_*gar 69
让我们看看为什么:
Gün*_*uer 36
<input type="text" [ngModel]="mymodel" (keypress)="mymodel=$event.target.value"/>
{{mymodel}}
Run Code Online (Sandbox Code Playgroud)
Sal*_*ani 20
处理此类情况的另一种方法是使用formControl并valueChanges在初始化组件时订阅它,这将允许您使用rxjs运算符来执行高级要求,例如执行http请求,应用debounce直到用户完成写句子,取最后一个值并省略以前,...
import {Component, OnInit} from '@angular/core';
import { FormControl } from '@angular/forms';
import { debounceTime, distinctUntilChanged } from 'rxjs/operators';
@Component({
selector: 'some-selector',
template: `
<input type="text" [formControl]="searchControl" placeholder="search">
`
})
export class SomeComponent implements OnInit {
private searchControl: FormControl;
private debounce: number = 400;
ngOnInit() {
this.searchControl = new FormControl('');
this.searchControl.valueChanges
.pipe(debounceTime(this.debounce), distinctUntilChanged())
.subscribe(query => {
console.log(query);
});
}
}
Run Code Online (Sandbox Code Playgroud)
Eme*_*nom 18
保持角度ngModel同步的秘密事件是事件调用输入.因此,您问题的最佳答案应该是:
<input type="text" [(ngModel)]="mymodel" (input)="valuechange($event)" />
{{mymodel}}
Run Code Online (Sandbox Code Playgroud)
小智 6
<input type="text" (keypress)="myMethod(myInput.value)" #myInput />
Run Code Online (Sandbox Code Playgroud)
存档.ts
myMethod(value:string){
...
...
}
Run Code Online (Sandbox Code Playgroud)
对于响应式表单,您可以订阅对所有字段或仅特定字段所做的更改。
获取 FormGroup 的所有更改:
this.orderForm.valueChanges.subscribe(value => {
console.dir(value);
});
Run Code Online (Sandbox Code Playgroud)
获取特定字段的更改:
this.orderForm.get('orderPriority').valueChanges.subscribe(value => {
console.log(value);
});
Run Code Online (Sandbox Code Playgroud)
小智 5
我设法使用以下代码在 Angular 11 中解决了这个问题:
<input type="number" min="0" max="50" [value]="input.to" name="to"
(input)="input.to=$event.target.value; experienceToAndFrom()">
Run Code Online (Sandbox Code Playgroud)
而且,这experienceToAndFrom()是我的组件中的一个方法。
PS:我尝试了以上所有解决方案,但没有成功。
| 归档时间: |
|
| 查看次数: |
352640 次 |
| 最近记录: |