简体   繁体   中英

window.open + Opening content in 2 tabs

I'm using angular js and javascript in my application.And here , when I click <a> tag it call a method in angular controller ,which has $window.open(); . Every time I click it content open in 2 tabs .What is the reason for that? Please help me out.

HTML

 <li><a href="#" id="test_home" ng-click="test_home()">Test Click</a></li>

Angular controller

controllers.controller('LogoutCtrl', ['$scope', '$location','$cookieStore','$rootScope','$window',
    function($scope, $location,$cookieStore,$rootScope ,$window) {

        $scope.test_home = function () {
            $window.open('www.yahoo.com'); 
         };             
    }
    ]);

Try using preventDefault:

$scope.test_home = function (e) {
            e.preventDefault();
            $window.open('www.yahoo.com'); 
         }; 

Or, use return false;

Nothing in common with $window.open , maybe some other event listener is bind to #test_home or some browser plugin is responsible for it.

Short example on jsfiddle shows that method will open only one window / tab.

angular.module('windowOpen', []).controller('open', function ($scope, $window) {
    $scope.openMe = function () {
        $window.open('http://google.com/');
    };
});

jsfiddle: http://jsfiddle.net/krzysztof_safjanowski/AKB4c/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM