我正在尝试通过 ng-click 在我的 Angular 应用程序中触发 $window.open(url, windowName, attributes);
由于 ng-click 链接到我模板上的按钮,我已经定义了一个指令并将 window.open 包装在一个函数触发器中:
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>
出于某种原因,当我单击按钮时窗口没有打开。我的代码有什么问题?
最佳答案
您不能使用链接注入(inject)窗口,您可以简单地使用原生 JavaScript window 对象
示例:
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>
关于javascript - 在指令中使用 window.open,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22592244/