2015-12-17 29 views
0

我想提出一个结账形式的外部URL,应该打开与结果一个新的窗口,但我不能让我的应用程序打开它也不在一个单独的窗口中,也不使用在应用程序浏览器。发表在新窗口中的表单/ inAppBrowser在离子(科尔多瓦)

我到目前为止所做的工作是用里面的表单创建一个指令,并从链接器函数中调用submit元素。

当我在浏览器中运行时,这会打开一个新窗口(就像我想要的)。 问题当设备运行出现,因为它只是REPLACES我有新的内容鉴于内容无需打开对外窗口

所以,问题是......在设备上运行时,如何张贴此窗体打开一个新窗口(导航器)或应用程序内浏览器,并显示结果?

谢谢!

+0

显示控制器。你如何援引IAB? – isherwood

+0

我包括一个简化的控制器,但无论如何,我没有调用IAB的形式(这是我不知道该怎么办) –

回答

4

好吧,弄清楚它很复杂,但最终解决方案“非常”简单。我会在这里发布它来帮助任何其他面临同样问题的人。如果有人有更优雅的解决方案,它将受到欢迎。

我结束做的事情就是:

  1. 打开我自己的模板,角度和我的形式新窗口。
  2. 在新窗口控制器,全局窗口对象
  3. 旧窗口中创建一个回调函数,在loadstop事件后,执行该回调
  4. 在新窗口张贴的形式,重定向到目标页面(这是我想要的)。

下面的代码(注意,我使用与形式的指令,这样我就可以控制何时提交,从链接功能,而这些数据是通过服务与指令共享):

angular.module('starter', ['ionic']) 
 

 

 
.constant('cartData', { 
 
     redirectUrl: 'https://test.mycart.com/hpp/pay.shtml', 
 
     redirectMethod: 'POST', 
 
     redirectData: { 
 
       'formParam1': 'value1', 
 
       'formPara2': 'value2' 
 
      } 
 
    } 
 
) 
 

 

 
.controller('InitCtrl', function($cordovaInAppBrowser, $scope, cartData) { 
 
     
 
    $scope.openView = function(){ 
 
    
 
     var counter = 0; 
 
     if(ionic.Platform.isWebView()){ 
 

 
      $ionicPlatform.ready(function() {      
 
       //this is the cordova in app web view 
 
       var ref = $cordovaInAppBrowser.open('payment.html', '_blank', {location:'yes'}); 
 

 
       $rootScope.$on('$cordovaInAppBrowser:loadstop', function(e, event){ 
 
        if(counter < 1){ 
 
         //prevent the callback to be called several times 
 
         $cordovaInAppBrowser.executeScript({ 
 
          code: 'window.paymentCallback('+ angular.toJson(cartData) +');' 
 
         }); 
 
         counter++; 
 
        } 
 
       }); 
 
      }); 
 
     } 
 
    }; 
 
    
 
}) 
 

 

 

 

 
//and then in payment.js 
 

 

 
window.paymentCallback = null; 
 

 
angular.module('payment', []) 
 

 

 
.directive('autoSubmitForm', ['$timeout', 'autoSubmitFormDelegate', function($timeout, autoSubmitFormDelegate) { 
 
    return { 
 
     replace: true, 
 
     scope: {}, 
 
     template: '<form action="{{formData.redirectUrl}}" method="{{formData.redirectMethod}}">'+ 
 
         '<div ng-repeat="(key,val) in formData.redirectData">'+ 
 
          '<input type="hidden" name="{{key}}" value="{{val}}" />'+ 
 
         '</div>'+ 
 
        '</form>', 
 
     link: function($scope, element, $attrs) { 
 
      
 
      autoSubmitFormDelegate.submitCallback = function(data) { 
 
       $scope.formData = data; 
 
       $timeout(function() { 
 
        element[0].submit(); 
 
       }); 
 
      }; 
 
     } 
 
    } 
 
}]) 
 

 
.factory('autoSubmitFormDelegate', function(){ 
 
    var delegate = {}; 
 
    
 
    delegate.submitCallback = null; 
 
    
 
    delegate.submitForm = function(data){ 
 
     if(delegate.submitCallback){ 
 
      delegate.submitCallback(data); 
 
     } 
 
    } 
 
    
 
    return delegate; 
 
}) 
 

 
.controller('PaymentCtrl', function($scope, $timeout, $window, $sce, autoSubmitFormDelegate){ 
 

 

 
    $window.paymentCallback = function(data){ 
 
     console.log("callback called"); 
 
     data.redirectUrl = $sce.trustAsResourceUrl(data.redirectUrl);  
 
     $timeout(function(){ 
 
      autoSubmitFormDelegate.submitForm(data); 
 
     }); 
 
    }; 
 
    
 
})
<link href="http://code.ionicframework.com/1.1.1/css/ionic.min.css" rel="stylesheet"> 
 
<script src="http://code.ionicframework.com/1.1.1/js/ionic.bundle.min.js"></script> 
 

 

 
<body ng-app="starter"> 
 

 
    <ion-view view-title="Init"> 
 
    <ion-content> 
 
    <h1>Init</h1> 
 
     <button class="button button-positive" ng-click="openView()">Open new view</button> 
 
    </ion-content> 
 
</ion-view> 
 
    
 
</body> 
 

 

 

 
<script type="text/ng-template" id="payment.html"> 
 

 
<html> 
 
    <head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> 
 
    <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'"> 
 
    <title></title> 
 

 
    <script src="../lib/angular/angular.min.js"></script> 
 
    <script src="../js/payment.js"></script> 
 
    </head> 
 

 
    <body ng-app="payment" ng-controller="PaymentCtrl"> 
 
    <auto-submit-form></auto-submit-form> 
 
    </body> 
 
</html> 
 

 
</script>

+0

非常感谢你的兄弟。 –

+0

离子2呢?任何链接? –