0

I need to get the search list using speciesName from the table and I tried to get the data while pressing enter key, but, it is giving me that input data is not work (undefined).

How can I pass the search value to the type script method in the same input?

The type script component is like this:

getSearchResult(speciesName) {

this.speciesName = speciesName;
this.speciesService.getSearchResult(speciesName).subscribe(result => {
  this.speciesList = result.results;
}, error => this.toastr.error(error.statusText, '', {
  timeOut: 3000
}));

}

and the html for the component is like this:

 <form class="form-inline" id="searchForm">
            <div class="form-group">
                <input class="form-control mr-sm-2" type="search" placeholder="Enter Species Name ..."
                    id="speciesNameSearch" name="speciesNameSearch" aria-label="Search"
                    (keyup.enter)="getSearchResult(speciesNameSearch)">
            </div>
        </form>

After I put the search criteria and input the Enter key it giving me not found because the value of speciesName is undefined.

How can I get speciesName value after I input it and click enter key and insert it to the search method?

2 Answers 2

1

You need to add [(ngModel)] attribute for databinding to work. Don't need to pass argument for the function.

Update the html code as below:

<form class="form-inline" id="searchForm">
            <div class="form-group">
                <input class="form-control mr-sm-2" type="search" placeholder="Enter 
                    id="speciesNameSearch" name="speciesNameSearch" 
                    (keyup.enter)="getSearchResult()" [(ngModel)]="speciesName"  >
            </div>
        </form>

The component function code:

//define speciesname for component
speciesName: string = "";

getSearchResult() {


this.speciesService.getSearchResult(this.speciesName).subscribe(result => {
  this.speciesList = result.results;
}, error => this.toastr.error(error.statusText, '', {
  timeOut: 3000
}));
Sign up to request clarification or add additional context in comments.

Comments

1

You are trying to pass the value from the form field by using name attribute. You can use [(ngModel)] or can pass the $event as the function argument and get the value in the component as event.target.value

Html Looks something like this :

 <form class="form-inline" id="searchForm">
        <div class="form-group">
            <input class="form-control mr-sm-2" type="search" placeholder="Enter Species Name ..."
                id="speciesNameSearch" name="speciesNameSearch" aria-label="Search"
                (keyup.enter)="getSearchResult($event)">
        </div>
    </form>

and in the component file change as :

getSearchResult(speciesName) {
this.speciesName = speciesName.target.value;
console.log("species name : ", this.speciesName);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.