1

I have a component (componentA) that is using another component (componentB) in its HTML. ComponentB has a button where the title is something else, but I would like to change the name of the button in ComponentA.

ComponentA uses componentB button to navigate to another page, but ComponentB has button that opens up a form. The navigation works and everything, but I would just like to change the name of the buttons when its on different page.

ComponentA.html

<div>
    <component-b (click)="buttonEdit()"></component-b>
</div>

ComponentA.ts

public buttonEdit(): void {
    this.router.navigate(['/users']);
  }

ComponentB.html

<button (click)="openModal()">Add Users</button>

ComponentB.ts

 @Input() buttonEdit: () => void;

2 Answers 2

2

You can try this Component B.tmpl:

<button (click)="openModal()">{{buttonName}}</button>

Component B.ts:

@Input() buttonEdit: () => void;
@Input() buttonName: string = 'Add Users';

Component A.ts:

public buttonEdit(): void {
  this.router.navigate(['/users']);
}
public buttonName(): string {
  ...
  return buttonName;
}

component A.tmpl:

<div>
    <component-b (click)="buttonEdit()" [buttonName]="buttonName()"></component-b>
</div>
Sign up to request clarification or add additional context in comments.

8 Comments

is there an easier way to do this in case I would like to fix its CSS in componentA?
You can use ngStyle
<div [ngStyle]="{'background-color':'green'}"></<div> this is example
it didn't work...it still taking the componentA background color
No, you should receive backgroundColor from component A like buttonName
|
2

Try this

Component A.tmpl:

<div>
    <component-b (click)="buttonEdit()" [buttonName]="buttonName()" [backgroundColor]="backgroundColor()"></component-b>
</div>

Component A.ts:

public backgroundColor(): string {
  ...
  return backgroundColor;
}

Component B.tmpl:

<div [ngStyle]="{'background-color': backgroundColor}"></<div>

Component B.ts:

@Input() backgroundColor: string = 'green';

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.