Is it possible to use anchor links with Angularjs?
I.e.:
<a href="#top">Top</a>
<a href="#middle">Middle</a>
<a href="#bottom">Bottom</a>
<div name="top"></div>
...
<div name="middle"></div>
...
<div name="bottom"></div>
Thank you
This question is related to
angularjs
I don't know if that answers your question, but yes, you can use angularjs links, such as:
<a ng-href="http://www.gravatar.com/avatar/{{hash}}"/>
There is a good example on the AngularJS website:
http://docs.angularjs.org/api/ng.directive:ngHref
UPDATE: The AngularJS documentation was a bit obscure and it didn't provide a good solution for it. Sorry!
You can find a better solution here: How to handle anchor hash linking in AngularJS
Works for me:
<a onclick='return false;' href="" class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" ng-href="#profile#collapse{{$index}}"> blalba </a>
$anchorScroll is indeed the answer to this, but there's a much better way to use it in more recent versions of Angular.
Now, $anchorScroll accepts the hash as an optional argument, so you don't have to change $location.hash at all. (documentation)
This is the best solution because it doesn't affect the route at all. I couldn't get any of the other solutions to work because I'm using ngRoute and the route would reload as soon as I set $location.hash(id)
, before $anchorScroll could do its magic.
Here is how to use it... first, in the directive or controller:
$scope.scrollTo = function (id) {
$anchorScroll(id);
}
and then in the view:
<a href="" ng-click="scrollTo(id)">Text</a>
Also, if you need to account for a fixed navbar (or other UI), you can set the offset for $anchorScroll like this (in the main module's run function):
.run(function ($anchorScroll) {
//this will make anchorScroll scroll to the div minus 50px
$anchorScroll.yOffset = 50;
});
You could try to use anchorScroll.
So the controller would be:
app.controller('MainCtrl', function($scope, $location, $anchorScroll, $routeParams) {
$scope.scrollTo = function(id) {
$location.hash(id);
$anchorScroll();
}
});
And the view:
<a href="" ng-click="scrollTo('foo')">Scroll to #foo</a>
...and no secret for the anchor id:
<div id="foo">
This is #foo
</div>
The best choice to me was to create a directive to do the work, because $location.hash()
and
$anchorScroll()
hijack the URL creating lots of problems to my SPA routing.
MyModule.directive('myAnchor', function() {
return {
restrict: 'A',
require: '?ngModel',
link: function(scope, elem, attrs, ngModel) {
return elem.bind('click', function() {
//other stuff ...
var el;
el = document.getElementById(attrs['myAnchor']);
return el.scrollIntoView();
});
}
};
});
You need to only add target="_self" to your link
ex. <a href="#services" target="_self">Services</a><div id="services"></div>
I had the same problem, but this worked for me:
<a ng-href="javascript:void(0);#tagId"></a>
You can also Navigate to HTML id from inside controller
$location.hash('id_in_html');
If you are using SharePoint and angular then do it like below:
<a ng-href="{{item.LinkTo.Url}}" target="_blank" ng-bind="item.Title;" ></a>
where LinkTo and Title is SharePoint Column.
Or you could simply write:
ng-href="\#yourAnchorId"
Please notice the backslash in front of the hash symbol
Source: Stackoverflow.com