How to implement an Alert in the angular HTML page?

Asked

Viewed 3,232 times

0

As you can see in the figure below the Angular component is composed of component, css and Html

inserir a descrição da imagem aqui

in the midia-cadastrar.component.html file tried to put a simple implementation of Alert as you can see below;

<script type="text/javascript">
  alert("alo tudo bem?");
  </script>

Being that it didn’t take, but if I put in the index.html file that is at the root of the project it picks up perfectly.

I want to be able to implement Alert on the page midia-cadastrar.component.html file, how could I do that?

2 answers

1


The most indicated guy in Angular applications is vc put all code javascript (typescript) in the midia-cadastrar.component.ts That’s what it’s for, HTML is for presentation only:

HTML

<button (click)="botaoClicado()">Clique</button>

JS - You create a method to fire Alert at the click of the button

export class midiaCadastrarComponent implements OnInit {
    botaoClicado() {
       alert('Botão clicado!');
    }
}

0

In the media-register file.component.html tries to do this..

<!DOCTYPE html>
<html>
<body>

<input type="button" value="Button" onclick="alerta()"/>

<script>
function alerta(){
	alert("alo tudo bem?");
}
</script>

</body>
</html> 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.