7

I googled several examples about using functions/variables from other components in Angular 4/5. They helped me a bit for understanding but I still couldn't figure out the solution to my problem.

I wanna call a standard JavaScript alert from another component.

I have a component, which has a variable like this:

center.ts

export class CenterPage {
  public message = new alert("Warning");

center.html

<products [message]="message"></products>

I'm using @Input to get the message in products.ts

export class ProductsComponent {
  @Input() message;

Now I want to show the alert, when the user clicks on a button in product.html

<button (click)="message"></button>

This seems to be wrong and I think this is not the right way how I'm trying to show the alert when the user clicks on the button.

How can I do this correctly?

EDIT

This is only one example. In the end I will have to call the same alert in several components. So I try to have only one function which I can call from all components so I won't have redundant code.

3
  • 1
    What's alert? Is that a class you created? Or are you trying to call a standard JavaScript alert() function? Commented Apr 5, 2018 at 8:23
  • Good question, totally missed the fact it could be a custom class. But as far as I know you should not be able to use alert as a class name since it's a built in function. Commented Apr 5, 2018 at 8:25
  • Oh sorry, I forgot, I will edit my post. It's the normal JavaScript standard Commented Apr 5, 2018 at 8:27

2 Answers 2

11

You should rather put the string into your component instead of the whole alert itself. Afterwards you can call the alert in your click handler.

Only pass the string to your child component.

export class CenterPage {
  public message = "Warning";

Handle the click within a dedicated function.

<button (click)="myFunc"></button>

Do your logic stuff in your function.

public myFunc() {
  alert(this.message);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, that's a good idea but the problem is, that I need to call this alert from several components and then I would have to add "myFunc()" in every single component. I'm trying to avoid redundant code
Use a Service instead. You'd just need to inject the service into all components.
Ya I found this solution too when I googled but it seems kinda "exaggerated" to me for just using an alert.. Isn't there a easier way?
You could also stick to your solution as long as you pass in a whole function (like myFunc()) to @Input() message. Yet this isn't the best approach. Best practice would be to use a Service instead.
2

You definitely have to use a service for such a functionality. You have to create a "service" folder in which you will create like a alert.service.ts file. in this file you will define the alert function you want to display in all your components.

Then you will import this service in the components which need it.

I suggest you to read this tuto from the angular documentation, which is really handy : https://angular.io/tutorial/toh-pt4

1 Comment

this is definitely wrong answer. The messages service in the tutorial just collects messages for presenting and its component with the same name grabs them via Rx and presents on the screen.

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.