Ole Albers Ole Albers - 1 year ago 60
Javascript Question

Use href with ng-click

I have a link that should call a $scope-function which causes another view to bei displayed.

But I also want that the User can rightclick onto that Link and open it into a new window or bookmark this.

So this is the (non-working) html:

<a ng-click="OpenSubsite(singleitem.Id)" href="{{GetUrl(singleItem.Id)}}">{{singleItem.Title}}</a>

The controller contains the following Code:

$scope.OpenSubSite=function(id) {
$scope.GetUrl=function(id) {
return "showitem.html#id="+id;

Both methods alone work fine. Just not in the combination I want. I want the "
" to be called when clicking onto that URL but when doing a rightclick ("open in new tab" or "add to favorites") the "
"-Returnvalue should be used instead.

But with this code, the URL from GetUrl() is always opened even on left Mousebutton click,

Is there a way to make this happen?

Answer Source

You have to prevent the default action when you trigger the ng-click. You can do that by passing the $event object of the ng-click directive:

<a ng-click="OpenSubsite($event, singleitem.Id)">..</a>

Then in the controller:

$scope.OpenSubSite = function(ev, id) {
    ev.preventDefault(); // prevent it opens default


    return false;