Optimist Rohit
Optimist Rohit

Reputation: 428

How to show previous input text after redirection to same page

I have a search component. After inputting text in search field and on enter key press ,search will happen and it will redirect to next component lets say showComponent, where result is shown.

I want when I go back from showComponent to search component , the previous input text must be there on the input text field. How can I achieve this ?

<div>
        <input type="text" class="form-control" id="search-text" aria-describedby="search-text" [formControl]="searchTextForm"
            placeholder="Search" autocomplete="off" (keyup.enter)="searchItem(searchTextForm.value)">
            <a (click)="searchItem(searchTextForm.value)">
                <span class="search-icon">
                    <i class="fa fa-search"></i>
                </span>
            </a>
        <ul *ngIf="showUl" class="list-group list-group-flush">
            <li  class="list-group-item" *ngFor="let product of productName | appFilter: searchTextForm.value">
                <a (click)="searchItem(product)">{{product}}</a> 
            </li>
        </ul>
    </div>

EDIT:-

I am using localStorage for storing the searched value . And retrieving it on ngOnInit using localStorage.getItem('searched');

Now how can I show it on input field as a predefined value ?

Upvotes: 0

Views: 210

Answers (1)

thisdotutkarsh
thisdotutkarsh

Reputation: 980

For setting the value from the localStorage to formControlDirective, do the following in the OnInit lifecycle hook of the app,

this.searchTextForm.setValue(localStorage.getItem('searched'));

Upvotes: 1

Related Questions