Angular 将更改时的输入设置为等于包含函数的变量
Angular set input on change equal to a variable holding a function
我知道你可以做类似的事情
<input (input)="doSomething($event)" />
<input (input)="boolVar = $event.target.value > 5" />
但是我怎样才能做到像
funcVar = (e) => {
console.log(e.target.value)
}
<input (input)="funcVar" />
我尝试了其他方法,例如
<input [input]="funcVar" />
<input [(input)]="funcVar" />
但运气不好。我这样做的原因是我们的表单是从数据集生成的,所以它们是我可以添加东西的唯一方法,比如通过将生成表单的变量传递东西。
事件处理程序应调用该函数。标记为:
<input (input)="funcVar($event)" />
对于 funcVar
成员定义为:
public funcVar = (e) => {
console.log(e.target.value)
}
如果稍后将另一个方法分配给 funcVar
,则将在触发 input
事件时执行该新方法:
someMethodExecutedLater() {
this.funcVar = (e) => {
// From now on, this new function will be called by the event handler
...
}
}
我知道你可以做类似的事情
<input (input)="doSomething($event)" />
<input (input)="boolVar = $event.target.value > 5" />
但是我怎样才能做到像
funcVar = (e) => {
console.log(e.target.value)
}
<input (input)="funcVar" />
我尝试了其他方法,例如
<input [input]="funcVar" />
<input [(input)]="funcVar" />
但运气不好。我这样做的原因是我们的表单是从数据集生成的,所以它们是我可以添加东西的唯一方法,比如通过将生成表单的变量传递东西。
事件处理程序应调用该函数。标记为:
<input (input)="funcVar($event)" />
对于 funcVar
成员定义为:
public funcVar = (e) => {
console.log(e.target.value)
}
如果稍后将另一个方法分配给 funcVar
,则将在触发 input
事件时执行该新方法:
someMethodExecutedLater() {
this.funcVar = (e) => {
// From now on, this new function will be called by the event handler
...
}
}