Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
68
rated 0 times [  73] [ 5]  / answers: 1 / hits: 35611  / 9 Years ago, wed, august 5, 2015, 12:00:00

Following the guide from the Angular2 site I have this html:



<input #something (keyup)=doneTyping($event)>
<button (click)=add(something .value)>Add</button>


with this controller:



@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;
}
}
}


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.


More From » angular

 Answers
34

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>


And later in the add function



add(st: HTMLInputElement){
this.stuff.push(st.value);
st.value = null;
}

[#65529] Monday, August 3, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
michaelashelbieh

Total Points: 303
Total Questions: 139
Total Answers: 97

Location: Suriname
Member since Sun, Oct 17, 2021
3 Years ago
michaelashelbieh questions
Sat, Nov 13, 21, 00:00, 3 Years ago
Fri, Sep 17, 21, 00:00, 3 Years ago
Tue, Sep 14, 21, 00:00, 3 Years ago
Mon, Aug 31, 20, 00:00, 4 Years ago
;