neptune neptune - 1 day ago 4
AngularJS Question

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.

Answer

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

Comments