角:2 - 如何清除一个输入与一个局部变量 [英] Angular 2 - How to clear an input with a local variable
本文介绍了角:2 - 如何清除一个输入与一个局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
继导弗朗的Angular2网站,我有这个网站:
Following the guide fron the Angular2 site I have this html:
<input #something (keyup)="doneTyping($event)">
<button (click)="add(something .value)">Add</button>
此控制器:
@Component({
selector: 'my-app',
appInjector: [SomeService]
})
@View({
templateUrl: 'index-angular',
directives:[NgFor]
})
class MyAppComponent {
name: string;
stuff: Array<string>;
constructor(someService: SomeService) {
this.name = 'Angular2Sample';
this.stuff= someService.getStuff();
}
add(st: string){
this.stuff.push(st);
}
doneTyping($event) {
if($event.which === 13) {
this.stuff.push($event.target.value);
$event.target.value = null;
}
}
}
当用户点击输入进,doneTyping方法清除输入, $ event.target.value = NULL;
When the user hits enter in the input, the doneTyping method clears the input with $event.target.value = null;
.
不过,我不能跟按下按钮后做同样的方式。
However I can't come with a way of doing the same after pushing the button.
推荐答案
您可以通过输入作为一个参数的按钮
You can pass the input as a parameter in the button
<input #something (keyup)="doneTyping($event)">
<!-- Input as paramter -->
<button (click)="add(something)">Add</button>
和后来的添加
函数
add(st: HTMLInputElement){
this.stuff.push(st.value);
st.value = null;
}
这篇关于角:2 - 如何清除一个输入与一个局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文