Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open new tab in browser by using $location, angular?

Tags:

angularjs

I have controller 'reportCtrl' and HTML that represents 6 tables with data and only several rows I show at once.

app.js

var appReport = angular.module('myAppReport', []);  appReport.config(['$routeProvider', function($routeProvider) {             $routeProvider.when('/showreport', {templateUrl: 'app/reports/reportsView.html', controller: 'reportCtrl'});             $routeProvider.when('/showreport/:type', {templateUrl: 'app/reports/reportsView.html', controller: 'reportCtrl'}); 

Each table has one button that on click, should open new tab in browser with extended table:

scope.onClick = function(path){     var real_path = $location.path() + '/' + path + '/';     $location.path( real_path );     // $window.open($location.path()); // doesn't work }; 

For example I have root view URL:

http://dev.test.com/reports/date/#/showreport 

Now, after button is pressed I want to create :

http://dev.test.com/reports/date/#/showreport/table_name 

and open it in new tab. As you see, I tried $window.open($location.path()); but it doesn't work, nothing happens.

Thanks,

like image 513
snaggs Avatar asked Dec 28 '13 21:12

snaggs


People also ask

Can you force a URL to open in a new tab?

You can make a HTML link open in a new tab by adding the target=”_blank” attribute. You should insert this after the link address.

Which tag is used to open a URL in a new tab?

You just need an anchor ( <a> ) element with three important attributes: The href attribute set to the URL of the page you want to link to. The target attribute set to _blank , which tells the browser to open the link in a new tab/window, depending on the browser's settings.

Is there a way to open links in a new tab without switching to that tab?

Use Keyboard with Mouse/Trackpad You can load any link in a new browser tab by clicking or tapping on it while holding down the Control key (Windows) or the Command key (Mac). Each tab loads in the background, so it's an ideal method to open multiple links as you move your way through a webpage.


2 Answers

You are opening a new window using another function which could get Angular confused.

Try passing the string in the $window.open(real_path) where real_path is a string containing the path you want to navigate

like image 96
Rayton Kiwelu Avatar answered Oct 03 '22 10:10

Rayton Kiwelu


If you want to open angular page in a new tab on ctrl+click or in same tab, then try this :-

HTML :

<a ng-click="navigationUrl($event)">My Link</a> 

JS:

    var navigationUrl = function (event) {             if (event.ctrlKey) {                 window.open(url, '_blank'); // in new tab             } else {                 $location.path(url); // in same tab             }         }; 
like image 44
Rubi saini Avatar answered Oct 03 '22 11:10

Rubi saini