【问题标题】:ionic read a local json file with $cordovaFileionic 使用 $cordovaFile 读取本地 json 文件
【发布时间】:2016-01-31 00:19:24
【问题描述】:

我正在尝试加载这样存储的本地 json 文件:

www/json/config.json

var path = '/android_asset/www/json'; //<-i have tried lots of differnt path's without success
var fileName = "config.json";
console.log('url: '+path+fileName);
console.log('file there?: '+angular.toJson($cordovaFile.checkFile(path, fileName)) );

$cordovaFile.readAsText(path, fileName).then(function (success) {
  console.log('read success: '+angular.toJson(success));
}, function (error) {
  console.log('read error: '+angular.toJson(error));
});

我将 ionic 与 ngCordova 插件 $cordovaFile 一起使用...有人有建议吗? 我只想用加载的“默认”json 来发送我的应用程序......

【问题讨论】:

    标签: android json path ionic ngcordova


    【解决方案1】:

    您无法使用带有该路径的$cordovaFile 读取www 文件夹中的文件,$cordovaFile 可以通过file:// 协议访问该文件。

    因为默认的 json 文件位于 www 文件夹中。以下是一些方法:

    1. 复制所有 JSON 内容并将其放入 javascript 文件中,并以全局变量的形式访问该文件。 (必须将此文件包含到index.html

      var json = {myKey: "myValue"};
      
    2. $resource 用作服务(未测试):

      $resource('file:///android_asset/www/json/myJson.json', {}, {
          query: { method: 'GET', params: {}, isArray: true }
      })
      
    3. 在控制器中使用$http

      $http.get('json/myJson.json')
      .success(function (data) {
          // The json data will now be in scope.
          $scope.myJsonData = data;
      });
      

    【讨论】:

      猜你喜欢
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 2019-05-29
      • 2018-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多