Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
32
rated 0 times [  37] [ 5]  / answers: 1 / hits: 60510  / 6 Years ago, mon, june 11, 2018, 12:00:00

So my code works when I hit enter (it runs the performSearch function successfully), but when I try to run the function by hitting my submit button I get the error:




cannot read property error of undefined




Here's my code:



<mat-form-field (ngSubmit)='performSearch($event)' color='primary' id='search-input' class='full-width' appearance='outline'>
<mat-label color='red'>Search</mat-label>
<input #searchBar matInput [(ngModel)]='searchValue' name='searchBar' [value]='searchValue' (keyup.enter)='performSearch($event)'>
</mat-form-field>

<button mat-raised-button color=primary (click)='performSearch(searchBar.value)' id='submit-search' type='submit' for='searchBar'>Submit</button>


All I want is a way to grab the #searchBar' value and pass it into the performSearch() function that fires when I click the button. How do I do that?


More From » angular

 Answers
5

You are doing two way binding in the search bar with var searchValue so you need to change only pass this var on click of submit.
Just replace your click event



(click)='performSearch(searchBar.value)' to
(click)='performSearch(searchValue)'

[#54224] Thursday, June 7, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deonkalvinw

Total Points: 409
Total Questions: 96
Total Answers: 89

Location: Saint Pierre and Miquelon
Member since Sun, Nov 27, 2022
2 Years ago
deonkalvinw questions
Sun, Feb 6, 22, 00:00, 2 Years ago
Tue, Dec 28, 21, 00:00, 2 Years ago
Sun, Aug 22, 21, 00:00, 3 Years ago
Sun, Mar 7, 21, 00:00, 3 Years ago
;