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.LoadItem(id);
}
$scope.GetUrl=function(id) {
return "showitem.html#id="+id;
}
Both methods alone work fine. Just not in the bination I want. I want the "OpenSubSite()
" to be called when clicking onto that URL but when doing a rightclick ("open in new tab" or "add to favorites") the "GetUrl()
"-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?
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.LoadItem(id);
}
$scope.GetUrl=function(id) {
return "showitem.html#id="+id;
}
Both methods alone work fine. Just not in the bination I want. I want the "OpenSubSite()
" to be called when clicking onto that URL but when doing a rightclick ("open in new tab" or "add to favorites") the "GetUrl()
"-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?
Share Improve this question edited Sep 20, 2016 at 12:59 Ole Albers asked Sep 20, 2016 at 12:53 Ole AlbersOle Albers 9,30513 gold badges82 silver badges179 bronze badges 4- consider using the directive in the answer of this question stackoverflow./questions/15731634/… – Gustavo Gabriel Commented Sep 20, 2016 at 12:55
- And what is the piece that doesn't work? – devqon Commented Sep 20, 2016 at 12:57
- I guess this does not work. I want no custom RMB-action, but "just" the URL to be submitted to the browser – Ole Albers Commented Sep 20, 2016 at 12:58
- @devqon clearified that in the question – Ole Albers Commented Sep 20, 2016 at 12:59
4 Answers
Reset to default 2You 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
$scope.LoadItem(id);
return false;
}
you need to prevent the default
ng-click="OpenSubsite($event,singleitem.Id)"
$scope.OpenSubSite=function($event,id) {
$event.preventDefault()
$scope.LoadItem(id);
}
Just add onclick="return false;"
the html will be
<a ng-click="OpenSubsite(singleitem.Id)" onclick="return false;" href="{{GetUrl(singleItem.Id)}}">{{singleItem.Title}}</a>
this will prevent the clicking to redirect to GetUrl but it will work in case you use open in new tab
An alternative answer: You can get $event
in ng-method and determine which button is clicked. After that, you can assign any method to related button like this:
var app = angular.module('app', []).controller('controller', function ($scope, $http) {
/*
other methods like LoadItem
*/
$scope.LoadItem = function(id) {
//some stuf here
}
$scope.OpenSubSite=function(id) {
$scope.LoadItem(id);
};
$scope.GetUrl=function(id) {
return "showitem.html#id="+id;
};
$scope.handleClick = function(evt, id) {
evt.preventDefault();
switch(evt.which) {
case 1:
alert("left click");
$scope.OpenSubSite(id);
// this is left click
break;
case 3:
alert("right click");
$scope.GetUrl(id);
// this is right click
break;
default:
break;
}
};
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="controller">
<a href="#" ng-mousedown="handleClick($event, 1)">link</a>
</div>
</div>