Open a new tab on button click in AngularJS

后端 未结 4 738
星月不相逢
星月不相逢 2020-12-02 15:06


openTab = function () {
  $http.post(\'www.google.com\');
}


        
相关标签:
4条回答
  • 2020-12-02 15:33

    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

    0 讨论(0)
  • 2020-12-02 15:35

    You should use the $location service

    Angular docs:

    • Guide to using $location
    • $location API
    0 讨论(0)
  • 2020-12-02 15:42

    I solved this question this way.

    <a class="btn btn-primary" target="_blank" ng-href="{{url}}" ng-mousedown="openTab()">newTab</a>
    
    $scope.openTab = function() {
        $scope.url = 'www.google.com';
    }
    
    0 讨论(0)
  • 2020-12-02 15:43

    Proper HTML way: just surround your button with anchor element and add attribute target="_blank". It is as simple as that:

    <a ng-href="{{yourDynamicURL}}" target="_blank">
        <h1>Open me in new Tab</h1>
    </a>
    

    where you can set in the controller:

    $scope.yourDynamicURL = 'https://stackoverflow.com';
    
    0 讨论(0)
提交回复
热议问题