Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a new tab on button click in AngularJS

<button type="button" class="btn btn-primary" ng-click="openTab()">new tab</button>  openTab = function () {   $http.post('www.google.com'); } 

What I want is post a require and open the response html in a new tab when you click the "openTab" button. There is no method to do this with $http. I think this maybe simple, but I can't find a way.

like image 529
neptune Avatar asked Jul 20 '13 01:07

neptune


People also ask

How to open new tab in AngularJS?

AngularJs open link in new tab – you'll use $window service to open a link in new tab in angularjs.

What is $window in AngularJS?

The $window service in AngularJS refer to the browser window object. With reference to JavaScript, window is a global object that includes many methods like prompt, conform, alert, etc. With window object in JavaScript, there is a testability problem due to it is defined as a global variable.


1 Answers

You can do this all within your controller by using the $window service here. $window is a wrapper around the global browser object window.

To make this work inject $window into you controller as follows

.controller('exampleCtrl', ['$scope', '$window',     function($scope, $window) {         $scope.redirectToGoogle = function(){             $window.open('https://www.google.com', '_blank');         };     } ]); 

this works well when redirecting to dynamic routes

like image 145
Aaron Avatar answered Sep 20 '22 08:09

Aaron