I'm trying to clear input text by pressing buttons which send routing event. So, if I'm getting specific route and no parameters (see green buttons on screen below), then text box should be cleared. If route is incorrect, or correct but with the parameter (white buttons), then nothing should happen. I subscribe text box value name$ to ActivatedRoute event (I'm using routing service to pass data between unrelated components).
Logic, which I'm trying to implement:
- if route is '/welcome' or '/products' - continue.
- if there are params - ignore, return nothing, don't change input text.
- if no params, return empty string (clear input with "").
Concise demo application: github.com/sam-klok/AngularPlayRoutes
HTML for main 1st component (menu):
<input #name type="text" [value]="(name$ | async)" placeholder="name"/>
HTML for 2nd/3rd component:
<button (click)="btnClear()">Clear input above</button>
Code from 1st component (which is currently not working) app.component.ts:
export class AppComponent {
name$ = this.activateRoute.queryParams.pipe(
map(params => params['name']),
map(x => {return x}))
Code from 2nd/3rd component:
export class WelcomeComponent implements OnInit {
constructor(private activateRoute: ActivatedRoute, private router: Router){}
btnClear(){
// we already on welcome page this should clear text input
this.router.navigate(['/welcome']);
}