0

I have 2 components : headerComponent and searchPageComponent.
Whatever the value we enter on header component search bar, the search page component should get the value.

header.component.html

<form class="form-inline md-form form-sm">
          <input class="form-control form-control-sm mr-3 w-75" id="searchString" type="text" 
             placeholder="Search" [(ngModel)]="searchValue" name="searchString" (ngModelChange)="onChange($event)">
          <button class="headerSearchbarBtn" id="btnSearch" routerLink="/searchResult">
              <span class="headerSearchbarIcon"></span>
           </button>
       </form>

header.component.ts

export class HeaderComponent implements OnInit {
        searchValue: string;
        constructor(private router: Router) { }
        ngOnInit() {
         }
         onChange($event) {
          this.searchValue = $event;
         }
       }

Search.component.ts

export class SearchComponent implements  OnInit {
        ngOnInit() {
          console.log(this.searchValue); // 'this.searchValue' => value should come from header.compomnent.ts 
        }
      }

There is no parent child relation here, I want to know how the data is passed from one component to other, and Why cant we directly access the input field box value in different components in Angular?

1

3 Answers 3

1

Service in add this code

 setSearchValue = new Subject<any>();
  getSearchValue = this.setSearchValue.asObservable();

First import your Service in Header component

constructor(private service: HeaderService)
header.component.ts
export class HeaderComponent implements OnInit {
        searchValue: string;
        constructor(private router: Router) { }
        ngOnInit() {
         }
         onChange($event) {
           this.service.setSearchValue.next(event);
           });
         }
       }
Search.component.ts
export class SearchComponent implements  OnInit {
        ngOnInit() {
         this.getSearchValue();
        }
      }

   getSearchValue(){
      this.service.getSearchValue.subscribe((value) => {
       this.searchValue = value;
      });
    }
Sign up to request clarification or add additional context in comments.

4 Comments

This works, But subscibe is not returning the value for the first time(it is not updating value - this.searchValue = value;), if I again enter some value on the input field, then only it is returning value.
@PavanRT Please add code in stackblitz and share link.
I have updated the code here - stackblitz.com/edit/angular-getinputvalue - @Vijay Prajapati
0

You need to use BehaviorSubject for without parent and child relation components. This link will help to you: https://stackblitz.com/edit/angular-behaviorsubject-example?file=src%2Fapp%2Fone%2Fone.component.html

Comments

0

You can use a service and a subject to communicate between sibling components

export class SharedService {

    private searchInputSource = new Subject<string>();

    searchInput = this.searchInputSource.asObservable();

    setSearchInput(value: string) {
        this.searchInput.next(value);
    }
}
export class HeaderComponent implements OnInit {
    searchInput: string;
    constructor(private sharedService: SharedService) { }
    ngOnInit() {
    }
    onChange($event) {
        this.sharedService.setSearchInput.next($event);
    });
}
export class SearchComponent implements OnInit, OnDestroy {

    privates subscription: Subscription;
    searchInput: string;

    ngOnInit(private sharedService: SharedService) {
        this.subscription = sharedService.searchInput.subscribe(
            value => {
                this.searchInput = value;
            });
    }

    ngOnDestroy() {
        this.subscription.unsubscribe();
    }
}

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.