最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular js $window.open not working in .success for mobile browser - Stack Overflow

programmeradmin1浏览0评论

I'm working with Angularjs and I got a problem. The function $window.open(/somewhere) works perfect on pc browsers and mobile browser. But there's 1 case it doesn't work. Please help to look below:

 $window.open("");  // OUTSIDE OF REQUEST - no problems 

 $https({
    type: "GET",
    url: "backendURL",
    data: jsonData,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        $window.open("");  //This doesn't work. 
    },
    error: function(msg) {
        //alert(error);
    }
});

Note that this is just happen with mobile browser : chrome and safari (I didn't test for others) So I think maybe someone has experience with this. Please please please help and advise.

Thanks ...

I'm working with Angularjs and I got a problem. The function $window.open(/somewhere) works perfect on pc browsers and mobile browser. But there's 1 case it doesn't work. Please help to look below:

 $window.open("https://www.myurl.");  // OUTSIDE OF REQUEST - no problems 

 $https({
    type: "GET",
    url: "backendURL",
    data: jsonData,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        $window.open("https://www.myurl.");  //This doesn't work. 
    },
    error: function(msg) {
        //alert(error);
    }
});

Note that this is just happen with mobile browser : chrome and safari (I didn't test for others) So I think maybe someone has experience with this. Please please please help and advise.

Thanks ...

Share Improve this question asked Jan 7, 2016 at 1:54 Lại Công ThànhLại Công Thành 331 silver badge5 bronze badges 3
  • What is with $window?? Pop Up blocker is probably blocking it. – epascarello Commented Jan 7, 2016 at 1:57
  • hi bro, I already turn off the Pop up blocker :( still not work :( as I said, out side the request, it works perfect :( but inside, can not :( – Lại Công Thành Commented Jan 7, 2016 at 3:10
  • I'm also facing the same issue right now. I got this very same issue in $.ajax request in normal javascript and got it fixed, but for angular I don't how to fix it. Tried the same way I do to normal js, didn't work. – Carrie Commented Jan 12, 2016 at 19:52
Add a ment  | 

2 Answers 2

Reset to default 3

I found this solution online, hope it will help you. do var win = window.open(); before the callback function, then inside .success, change url by doing win.location = url;. This should do the trick.

I think the overarching problem has to do with the "this" scope in JavaScript. If you are able to use arrow functionsfrom the ES6 version of JavaScript you can simply do

success: (msg)=>{
  $window.open("https://www.myurl.");
}

I imagine most people will not have this option. In which case you can do something similar to what Yabin suggested:

//somewhere else in code.
var scopedWindow = $window;
//
success: function(msg){
  scopedWindow.open("https://www.myurl."); 
}
发布评论

评论列表(0)

  1. 暂无评论