angularjs: trigger ng-click function when user right click and select open in new tab


username

I'm currently defining an anchor tag like

<a href="" ng-click="controllerFn(scopeVar)">Click here</a>

Inside the controller function I use ng-storage ($localStorage to set some values ​​and then use the angular-ui router to go to the next state. The new link will open in a new tab.

$scope.controllerFn = function(var){
                    $localStorage.headings = $localStorage.headings || {};
                    $localStorage.headings[var.uuid] = var;
                    var url = $state.href('newstate', {id: var.uuid, name : var.name});
                    window.open(url,'_blank');
                };

This works as expected.

However, when the user right-clicks on the anchor tag and selects "open in new tab", the button will not work and the user will be taken to the main landing page instead of the new route.

When the user chooses to open in a new tab, I want to call "controllerFn" so that the $localStorage value is set before entering a new state and the link should open in a new tab.

Is that possible? Any ideas?

Kasper Kuss

The problem might be that you are using an angular app, which means your app is initialized on every page (re)load. Usually you don't actually refresh the page, Angular just edits the address bar (route) without actually reloading the page. When you open a new tab, your app is loaded and your current session/user object cannot be found.

Typically, you will have a session context created by the server. When you use $window.Session to store data, the data itself is only available in the current window. As I can see, opening a new tab will cause a new session to be created.

So to make it work, you have to store some kind of token and make sure to check if the token exists/valid every time the app starts.

angular.module('myApp',[..]).config(function(){
         //read token 
})

Related


Right click on svg to open new tab with element

Yukulele With firefox, when I middle-click (or ctrl+click) on an useelement , it will open the url xlink:hrefin a new tab (like href) Bug or feature? <svg viewBox="0 0 400 80"> <circle id="circle" cx="40" cy="40" r="30" fill="#29e"></circle> <use xlink

Right click on svg to open new tab with element

Yukulele With firefox, when I middle-click (or ctrl+click) on an useelement , it opens the url xlink:hrefin a new tab (like href) Bug or feature? <svg viewBox="0 0 400 80"> <circle id="circle" cx="40" cy="40" r="30" fill="#29e"></circle> <use xlink:hre

Right click to enable "open in new tab/window"

Michael Chodakis My link is a javascript function that shows the loader and then navigates to the target link: <script> function go(url) { document.body.innerHTML = "some loader html"; window.location = url; } </script> <a href="javascript:go('test.php');"

Right click and save link to open in new tab

Dishu I added a custom right click menu using jquery in my project, now what I want to achieve is: -> If anyone right clicks on the link, the option will say "Open link in new tab", when that option is selected, it will open in a new tab with that link. -> Oth

Right click on svg to open new tab with element

Yukulele With firefox, when I middle-click (or ctrl+click) on an useelement , it opens the url xlink:hrefin a new tab (like href) Bug or feature? <svg viewBox="0 0 400 80"> <circle id="circle" cx="40" cy="40" r="30" fill="#29e"></circle> <use xlink:hre

Right click to enable "open in new tab/window"

Michael Chodakis My link is a javascript function that shows the loader and then navigates to the target link: <script> function go(url) { document.body.innerHTML = "some loader html"; window.location = url; } </script> <a href="javascript:go('test.php');"

How to open new window browser when user click close tab?

end I tried this code to open new window browser when user click close tab but it doesn't work <script> window.onbeforeunload = function(e) { window.open("http://google.com"); window.stop(); }; </script> So, can you give me any way to do this? Quentin y

How to open new window browser when user click close tab?

end I tried this code to open new window browser when user click close tab but it doesn't work <script> window.onbeforeunload = function(e) { window.open("http://google.com"); window.stop(); }; </script> So, can you give me any way to do this? Quentin y

jquery right click when user tries to open new window

Chris Hough I have a Bootstrap modal with a simple button link. Currently, I am working on the following: jQuery('#jobPostWarning').find('a').mousedown(function(e) { if(e.which === 3) { jQuery(this).find('a').trigger("click"); } }); The purp

Open a new tab after button click in AngularJS

Neptune: <button type="button" class="btn btn-primary" ng-click="openTab()">new tab</button> openTab = function () { $http.post('www.google.com'); } What I want is to post a requirement and then open the response html in a new tab when the "openTab" button

Open a new tab after button click in AngularJS

Neptune: <button type="button" class="btn btn-primary" ng-click="openTab()">new tab</button> openTab = function () { $http.post('www.google.com'); } What I want is to post a requirement and then open the response html in a new tab when the "openTab" button

Open a new tab after button click in AngularJS

Neptune: <button type="button" class="btn btn-primary" ng-click="openTab()">new tab</button> openTab = function () { $http.post('www.google.com'); } What I want is to post a requirement and then open the response html in a new tab when the "openTab" button

Customize right click anchor tag to open link in new tab

usman610 I have the following code to open a specific right click menu for <a>an element . On any link I right click and click to open in a new tab, it only opens the first link. Counters in java-script are related to my inability to do so. hrefI want to open

Open link in AngularJS $state in new tab with ctrl + click

utkash sharma I'm trying to open a link in a new tab, just like when opening a normal link ctrl + clicked, except it appends a ui-sref directive which generates a ui-sref directive with an href attribute /stateName?param1=test&param2=test, since the tab is ope

Open link in AngularJS $state in new tab with ctrl + click

utkash sharma I'm trying to open a link in a new tab, just like when opening a normal link ctrl + clicked, except it appends a ui-sref directive that generates a ui-sref directive with an href attribute /stateName?param1=test&param2=test, since the tab is open

Open link in AngularJS $state in new tab with ctrl + click

utkash sharma I'm trying to open a link in a new tab, just like when opening a normal link ctrl + clicked, except it appends a ui-sref directive that generates a ui-sref directive with an href attribute /stateName?param1=test&param2=test, since the tab is open

Open link in AngularJS $state in new tab with ctrl + click

utkash sharma I'm trying to open a link in a new tab, just like when opening a normal link ctrl + clicked, except it appends a ui-sref directive that generates a ui-sref directive with an href attribute /stateName?param1=test&param2=test, since the tab is open

Open link in AngularJS $state in new tab with ctrl + click

utkash sharma I'm trying to open a link in a new tab, just like when opening a normal link ctrl + clicked, except it appends a ui-sref directive that generates a ui-sref directive with an href attribute /stateName?param1=test&param2=test, since the tab is open

CakePHP open to new tab on click

mark I have a function in my app where user can upload files to web server. These uploaded files will then appear in another page where another type of user can click the link. After clicking the link, a new tab will open and the file will be displayed. But I

Click the button to open a new tab

Nathan Wilson I want to create a link with a button that opens in a new tab. The code I'm currently using is currently opening a blank tab and I'm not sure why. <div class="text text-left"> <h2>WORK</h2> <h1>Title</h1> <p>Lorem ipsum dolor sit am

Click the button to open a new tab

Nathan Wilson I want to create a link with a button that opens in a new tab. The code I'm currently using is currently opening a blank tab and I'm not sure why. <div class="text text-left"> <h2>WORK</h2> <h1>Title</h1> <p>Lorem ipsum dolor sit am

CakePHP open to new tab on click

mark I have a function in my app where user can upload files to web server. These uploaded files will then appear in another page where another type of user can click the link. After clicking the link, a new tab will open and the file will be displayed. But I

Alert on button click and move user to open new tab

Dashan I created a button with a link that opens in a new tab. I also used JavaScript for reminders. Currently this code works fine. But after clicking OK in the Alert, the user stays on the same page. But I want to move the user to the newly opened tab. is it