我在做什么:在我的角应用中,当用户点击链接时,我从后端获取网址并使用$window.open()
启动。
问题: Safari弹出窗口拦截器阻止$window.open()
。如果我在服务调用的成功回调之外和控制器中$window.open()
,它运行良好。如何在服务成功回调中使用它?
Plunker :http://plnkr.co/edit/aI4rvJJhP2XC5Mk1nK3t?p=preview
代码(相关部分):
的index.html:
<body ng-app='myApp'>
<div ng-controller="MyCtrl">
<a href="javascript:;" ng-click="openLink()" target="_blank">Open Link</a>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="app.js"></script>
</body>
app.js:
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', ['$scope', 'sampleService', '$window', '$log', function($scope, sampleService, $window, $log) {
$scope.openLink = function() {
// call the service
sampleService.getURL().then(function(response) {
$log.info('service response', response); // just checking the response
$window.open(response, '_blank'); // this does not work! :(
}, function(error) {
$log.info('ERROR: ', error);
});
// $window.open('https://www.google.com', '_blank'); // works if placed here!
};
}]);
myApp.service('sampleService', ['$q', function($q) {
this.getURL = function() {
return $q(function(resolve, reject) {
setTimeout(function() {
resolve('https://www.google.com'); // static URL, for now
}, 2000); // send it back in 2 sec.
});
};
}]);
我该如何解决这个问题?