How to use anchor in Angularjs?

Asked

Viewed 490 times

2

I am starting to work with Angularjs and I have the following situation I have a href that redirects to a div inside the page itself by the id of the div:

<a href="#teste"></a>
<div id="teste"></div> 

but Angularjs tries to read this test called by href as if it were a view, does anyone know how to get around it? Maybe something that will make href identify that he is an anchor and not a view, or else that you know is also welcome.

  • We’re not missing the # before? Like: href="#teste"

  • You can use a component router to manage your application routes. That documentation can help you with initial implementation

2 answers

4

This probably has nothing to do with Angular, only the # in the href.

See working:

html {
  scroll-behavior: smooth;
}

div {
  width: 90%;
  height: 120vh;
  background-color: red;
}

.blue {
  background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<a href="#teste">Ir para DIV</a>
<div></div>
<div id="teste" class="blue">
</div>

  • So that’s my problem. When I put #testing on href. my project tries to read a view, and tries to redirect to a page whose address is my local address /test

  • I was able to solve, I created an isLogin in the $Scope of my login controller and instead of using href, I just call the controller method and updated this isLogin to true or false, and I can enable by *ngIf the login tab or the registration tab.

0

you can use a target="_self" on your href that will prevent the action by Angularjs

<a href="#teste" target="_self"></a>
<div id="teste"></div> 

Browser other questions tagged

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