javascript - How to load XHR requests in Cordova relative to the base href? -


i using angularjs , have like:

myapp.config([   '$stateprovider', '$urlrouterprovider', '$locationprovider', '$sceprovider', 'appconstants', function($stateprovider, $urlrouterprovider, $locationprovider, $sceprovider, appconstants) {     $sceprovider.enabled(false);     $urlrouterprovider.otherwise('/login');     $stateprovider.state('login', {       url: '/login',       templateurl: "/templates/login.html"     }).state('tos', { 

when loads in cordova, tries file:///templates/login.html should file:///users/ssiddiqui/library/developer/coresimulator/devices/5bfa4f09-2c0a-4916-9d08-21d8bdc9e0a8/data/containers/bundle/application/12b77264-bcb1-4daa-b1fa-8bc4033adfac/helloworld.app/www/templates/login.html since:

<base href="file:///users/ssiddiqui/library/developer/coresimulator/devices/5bfa4f09-2c0a-4916-9d08-21d8bdc9e0a8/data/containers/bundle/application/12b77264-bcb1-4daa-b1fa-8bc4033adfac/helloworld.app/www/">

so how can make happen?

in http-interceptor, request-interceptor, can tweak url if ends .html

steps

  1. get hosting address

    var hostpath = document.location.pathname.substring(0, document.location.pathname.length - 1);` 
  2. change url if ends .html

        if (config.url.indexof(".html") !== -1) {         config.url = hostpath + config.url;     } 

the complete request-interceptor looks this

    var requestinterceptor = function (config) {         var hostpath = document.location.pathname.substring(0, document.location.pathname.length - 1);         if (config.url.indexof(".html") !== -1) {             config.url = hostpath + config.url;         }         return config || $q.when(config);     }; 

setup

csapp.factory('myhttpinterceptor', function ($q){     var requestinterceptor = function (config) {         var hostpath = document.location.pathname.substring(0, document.location.pathname.length - 1);         if (config.url.indexof(".html") !== -1) {             config.url = hostpath + config.url;         }         return config || $q.when(config);     };     return {        request: requestinterceptor     } })  csapp.config(function(){          $httpprovider.interceptors.push('myhttpinterceptor');  }) 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -