href causes unintended page reload with Angularjs and Twitter Bootstrap

25,989

Solution 1

The hashbang in Angular is used for routing. Look at the tutorial for a deeper insight into how it works here.

You should also take a look at Angular UI Bootstrap.

Regular Boostrap wasn't built with Angular in mind, so there are few things that aren't in line with Angular. So the team decided to port Boostrap into Angular directives, giving you the ability to fully use Angular's ng- features (which you wouldn't be able to do easily with just regular Boostrap).


Due to the way routing works, I don't think you would be able to do what you want, and you shouldn't need to. Since you're using the <a> as a button, make it a regular button and add an ng-click:

<button class="btn btn-primary" ng-click="openDialog()">Open Dialog</button>

This is the Angular way (and how Angular UI Bootstrap works).

Lastly, in Angular, the <a> is a directive docs here, so if you want to prevent the default click, leave href="":

<a href="" ng-click="model.$save()">Save</a>

Solution 2

This is because of HTML link rewriting of Angular, explained here.

To prevent rewriting of location, add target=_self to those bootstrap links.

So instead of <a href="#myModal"> you need <a href="#myModal" target="_self">

Solution 3

I solved: Simply just change HREF attribute by the DATA-TARGET attribute.

<div>
    <!-- Nav tabs -->
    <ul class="nav nav-tabs" role="tablist" id="myTabs">
        <li role="presentation" class="active">
            <a data-target="#home" aria-controls="home" role="tab" data-toggle="tab">Home</a>
        </li>
        <li role="presentation">
            <a data-target="#profile" aria-controls="profile" role="tab" data-toggle="tab">Profile</a>
        </li>
    </ul>

    <!-- Tab panes -->
    <div class="tab-content">
        <div role="tabpanel" class="tab-pane active" id="home">Tortor Porta Sit</div>
        <div role="tabpanel" class="tab-pane" id="profile">Duis mollis, est non commodo</div>
    </div>
</div>

Solution 4

There's a simpler solution here: How to prevent redirecting <a href="#something"> in Angular Js1.2

Just add target="_self" to your links

Solution 5

I had similar issues, full page reload vs just view switching.

Because Angular apps are SAPs, we only update whatever comes after hostname/#/.

I instantiate a SuperController, kind of the application level controller and like Carlos V said, you can use ng-click instead of href to update $location.path().

Here's a fiddle link

Might not be the most optimal solution. Hope it helps.

Share:
25,989
user2375809
Author by

user2375809

Updated on July 25, 2020

Comments

  • user2375809
    user2375809 almost 4 years

    I am working on a project that uses Angularjs and Twitter Bootstrap.

    Bootstrap uses # to toggle components such as popover, modal etc. for example:

    <a href="#myModal" role="button" class="btn" data-toggle="modal">Launch demo modal</a>
    
    <!-- Modal -->
    <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="myModalLabel">Modal header</h3>
      </div>
      <div class="modal-body">
        <p>One fine body…</p>
      </div>
      <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
        <button class="btn btn-primary">Save changes</button>
      </div>
    </div>
    

    The problem is when I click on button with such href attribute, it causes full page reload, which means, everything in current page is lost. Is there a way to prevent this?

    some extra info:

    when i hover on the button, the url is weird. for example, my current page's url is

    localhost:8080/#/account
    

    the button's href is

    href="#myModal"
    

    I expect to see url

    localhost:8080/#/account#myModal
    

    However, what I see is

    localhost:8080/#myModal
    

    I am not sure if this is related to my problem.

    Thanks in advance!

    EDIT 1

    I have seen the other post that Stewie talked about. It explains html5mode and hashbang in angularjs, but it doesn't really solve my problem.

    I tried putting html5mode, and it still reloads the page when i click on the button

  • duckegg
    duckegg over 10 years
    It seems <a href="" ng-click=""> will prevent the ng-click operation in Android 2.3.x browser
  • firecape
    firecape about 7 years
    Thanks, this was perhaps the easiest solution!
  • Syed Faizan
    Syed Faizan almost 7 years
    Thank you, you saved my time :)
  • Tony
    Tony about 5 years
    Working great on Angular 7.