[英]Use window.open in a directive
我试图触发$window.open(url, windowName, attributes);
在我的角度应用程序中单击ng
我已经定义了一个指令并在函数触发器中包装window.open,这要感谢ng-click链接到模板上的按钮:
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中:
<button type="submit" class="cta main right ease"ng-click="openWindow()">open window</button>
由于某些原因,当我单击按钮时,窗口无法打开。 我的代码有什么问题?
您不能使用链接注入窗口,而只能使用本机JavaScript 窗口对象
例:
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>
您应该这样做:
myApp.directive('myModal', ['$log', '$window', function ($log, $window) {
return {
restrict: 'E',
templateUrl: 'modal-tpl',
replace: true,
transclude: true,
link: function (scope) {
scope.openWindow = function(){
$window.open('https://www.google.pl', 'Google', 'width=500,height=400');
//some other code
};
}
};
}]);
$ window服务是一个指令依赖项,它将在链接函数中可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.