Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

typescript - How can I let an angular component know which button was clicked to route to it?

I have an angular component which should display different information depending on which button was clicked to route to it. If button 1 was clicked I want to set button1=true in component2 and use an ngIf to display the correct information. If button 2 was clicked I want to set button2=true and use an ngIf to display the correct information.

The two components have different urls and are not parent/child of each other.

The code below is a barebones example of what I am trying to achieve.

How can I communicate to component 2 whether button 1 or button 2 was pressed?

comp1.component.ts

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-comp1',
  templateUrl: './comp1.component.html',
  styleUrls: ['./comp1.component.css']
})
export class Comp1Component implements OnInit {

  constructor(private router: Router,) { }

  public onButton1(){
    this.router.navigate(['/comp2']);
  }

  public onButton2(){
    this.router.navigate(['/comp2']);
  }

  ngOnInit() {
  }

}

comp1.component.html

<div>
    <button (click)="onButton1()">Button1</button>
    <button (click)="onButton2()">Button2</button>
</div>

comp2.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-comp2',
  templateUrl: './comp2.component.html',
  styleUrls: ['./comp2.component.css']
})
export class Comp2Component implements OnInit {

  public button1 = false;
  public button2 = false;

  constructor() { }

  ngOnInit() {
    // if button pressed == "button1" in component 1
    this.button1 = true

    // if button pressed == "button2" in component 1
    this.button2 = true
  }

}

comp2.component.html

<div *ngIf="button1">
    Button 1 was clicked
</div>

<div *ngIf="button2">
    Button 2 was clicked
</div>

app-routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { Comp1Component } from './comp1/comp1.component';
import { Comp2Component } from './comp2/comp2.component';

const routes: Routes = [
  { path: 'comp1', component: Comp1Component },
  { path: 'comp2', component: Comp2Component }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

app.component.html

<router-outlet></router-outlet>
question from:https://stackoverflow.com/questions/65844144/how-can-i-let-an-angular-component-know-which-button-was-clicked-to-route-to-it

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could send a parameter in the route which could then be fetched in component 2 using ActivatedRoute. If you expect to use only 2 buttons, then it could just be a boolean flag to denote the condition. If not you could think about using other methods to denote states (like enums).

I'll illustrate with a boolean

Component 1

export class Comp1Component implements OnInit {
  public onButton1(){
    this.router.navigate(['/comp2', {state:true}]);
  }

  public onButton2(){
    this.router.navigate(['/comp2', {state:false}]);
  }
}

Component 2

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

export class Comp2Component implements OnInit {
  public state = false;

  constructor(private route: ActivatedRoute) { }

  ngOnInit() {
    this.state = this.route.snapshot.paramMap.get('state');
  }
}

Component 2 Template

<div *ngIf="state; else otherState">
    Button 1 was clicked
</div>

<ng-template #otherState>
    Button 2 was clicked
</ng-template>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...