2015-11-06 55 views
0

我讀到,如果用戶單擊鏈接,則可以使用JavaScript打開它,並且它不會被彈出窗口阻止程序阻止。你可以看到,在這裏:用戶動作的鏈接被當作彈出窗口並被阻止

Allow window.open to open new window and not popup

所以,棱角分明,我創造了這個指令:

.directive('kdExport', function() { 

    return { 
     restrict: 'A', 
     scope: { 
      options: '=kdExport' 
     }, 
     controller: 'ExportImageController', 
     link: function (scope, element, attrs, controller) { 

      // Bind to the onclick event of our button 
      element.bind('click', function (e) { 

       // Prevent the default action 
       e.preventDefault(); 

       // Copy our options 
       angular.extend(controller.options, scope.options); 

       // Generate the image 
       controller.generateImage(function (preview) { 

        // Create our url 
        var url = '/kits/preview/' + preview.id; 

        // Open a new window 
        window.open(url, '_blank'); 
       }); 
      }); 
     } 
    }; 
}) 

的問題是,它被封鎖在IE中。 如何防止IE阻止此鏈接?

回答

0

在用戶交互(點擊)之後,必須在事件發生後立即打開彈出框

在這裏,您在controller.generateImage的回調函數中執行window.open。

試着在你的angular.extend之後移動你的代碼,它會很好地工作。

[...] 
link: function (scope, element, attrs, controller) { 

     // Bind to the onclick event of our button 
     element.bind('click', function (e) { 

      // Prevent the default action 
      e.preventDefault(); 

      // Copy our options 
      angular.extend(controller.options, scope.options); 

      // Open a new window here, not in a callback function 
      window.open(url, '_blank'); 
     }); 
    } 
[...] 
+0

好吧,我的代碼執行完成後(generateImage有承諾),我怎麼能打開窗口。 – r3plica

+0

@ r3plica你不能在承諾中做到這一點,因爲它不僅僅是在用戶操作之後 –

相關問題