简体   繁体   中英

Using ngView directive without hash in angularjs

AngularJS Part:

myApp.config(['$routeProvider','$locationProvider',function($routeProvider,$locationProvider) {
    $routeProvider.
    when('/main', {templateUrl: 'sub/main.tpl', controller: MainCtrl}).
    when('/users', {templateUrl: 'sub/users.tpl', controller: UserCtrl}).
    otherwise({redirectTo: '/main'});
}]);

and my index.html

<a href="#/main">Main</a> | <a href="#/users">Display Users</a>
<div ng-view></div>

It works great but I am having problem with hashes.

When I try to use an anchor ie:

<a href="#test">test</a>

when I click on this anchor it is calling the main page.

Is it possible to get rid of the hashes when we use ng-view?

Ah, because AngularJS reroutes your urls. Try using ng-href,

<a ng-href="#test">test</a>

Edit: You can try changing the id of the anchor to the angular url..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM