Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用户操作的链接被视为弹出窗口并被阻止_Javascript_Angularjs - Fatal编程技术网

Javascript 用户操作的链接被视为弹出窗口并被阻止

Javascript 用户操作的链接被视为弹出窗口并被阻止,javascript,angularjs,Javascript,Angularjs,我了解到,如果用户单击一个链接,那么您可以使用JavaScript打开它,它不会被弹出窗口拦截器阻止。你可以在这里看到: 因此,在angular中,我创建了这个指令: .directive('kdExport', function () { return { restrict: 'A', scope: { options: '=kdExport' }, controller: 'ExportIma

我了解到,如果用户单击一个链接,那么您可以使用JavaScript打开它,它不会被弹出窗口拦截器阻止。你可以在这里看到:

因此,在angular中,我创建了这个指令:

.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阻止此链接?

在用户交互(单击)后打开弹出窗口必须在事件发生后进行。

在这里,您可以在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');
        });
    }
[...]

好的,那么我如何在代码执行后打开窗口(generateImage中有承诺)。@r3plica你不能在承诺中这样做,因为这不仅仅是在用户操作之后