在指令中使用window.open [英] Use window.open in a directive

查看:234
本文介绍了在指令中使用window.open的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图触发 $ window.open(URL,windowName,属性); 在我与NG-点击角度应用

I am trying to trigger $window.open(url, windowName, attributes); in my angular app with a ng-click

我已经定义的指令,并在功能触发包window.open多亏了NG-点击链接到一个按钮在我的模板:

I have defined a directive and wrap window.open in a function trigger thanks to an ng-click linked to a button on my template:

myApp.directive('myModal', ['$log', function ($log, $window) {
    return {
        restrict: 'E',

        templateUrl: 'modal-tpl',

        replace: true,

        transclude: true,

        link: function (scope, window) {
            scope.openWindow = function(){
                window.open('https://myLink', 'Google', 'width=500,height=400');
               //some other code
            };
        }
    };
}]);

在我的HTML:

And in my HTML:

   <button type="submit" class="cta main right ease"ng-click="openWindow()">open window</button>

由于某种原因,窗口不会当我点击该按钮打开。这有什么错我的code?

For some reason the window doesn't open when I click on the button. What's wrong with my code?

推荐答案

您无法使用环节注入窗口,你可以简单地使用本地JavaScript的窗口对象

You cannot inject window using link, you can simply use the native JavaScript window object

例如:

JS:

var app=angular.module('App', []);
app.directive('myModal', ['$log', function ($log) {
    return {
        restrict: 'EA',

        link: function (scope,element) {
            scope.openWindow = function(){
                window.open('https://myLink', 'Google', 'width=500,height=400');
               //some other code
            };
        }
    };
}]);

HTML

<div ng-app="App"  >
 <button type="submit" my-Modal="" class="cta main right ease"ng-click="openWindow()">open window</button>
</div>

活生生的例子: http://jsfiddle.net/choroshin/crt45/1/

这篇关于在指令中使用window.open的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆