简体   繁体   中英

Open link in new tab on image click in angular without popup blocker

I have setup ng-click on a image in my web page. In the ng-click handler, I use

    ServiceData.getProductDetails(product).then(function(data) {
        $scope.url = data;
            var win = window.open($scope.url, '_blank');
            win.focus();
    });

    getProductDetails: function(product) {
        var promiseProductDetails;
        if (productDetailsArr[product.id] == undefined) {
            var ajaxUrl = productsByIDarray[product.id]['detailsview'];
            try {

                // $http returns a promise, which has a then function, which also returns a promise
                promiseProductDetails = $http.get(ajaxUrl).then(function(response) {
                    return response;
                });
            } catch(err) {
            }

            // Return the promise to the controller
            return promiseProductDetails;
        } else {
            var deferred = $q.defer();
            deferred.resolve(productDetailsArr[product.id][url]);
            return deferred.promise;
        }

    }

So the click initiates an ajax call to fetch the URL where user should go (the target url is dynamic). I have made the ajax call as sync using angular promise. This results in the web browser treating it as untrusted and blocks the popup. I don't want to enclose the img tag with anchor a tag in html. Do I have any option available to avoid the popup blocker?

解决此问题的方法是将ajax调用移至用户与页面交互的较早点。

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