0

开发环境

cju:~/Projects/ionic2/i2ts $ ionic info
WARN: ionic.config.js 已被弃用,您可以将其删除。
您的系统信息:
Cordova CLI:6.1.1
Gulp 版本:CLI 版本 3.9.1
Gulp local:本地版本 3.9.1
Ionic Framework 版本:2.0.0-beta.6
Ionic CLI 版本:2.0.0-beta.25
Ionic App Lib 版本:2.0.0-beta.15
ios-deploy 版本:1.8.5
ios-sim 版本:5.0.8
操作系统:Mac OS X El Capitan
节点版本:v4.4.2
Xcode 版本:Xcode 7.3 Build 版本 7D175

问题

我在浏览器平台和ios手机上都试过了。要在浏览器平台上查看它,请执行以下操作

离子平台添加浏览器

离子运行浏览器

在浏览器上,它从“Hello Ionic”页面开始。单击“使用cordova-plugin-file 写入本地文件”按钮,在背面,“这是新文本”将使用cordova-plugin-file 写入本地文件。单击“使用cordova-plugin-file 从文件读取”按钮,控制台日志显示文件已读回。但是,视图没有更新。再次单击任何按钮,视图将更新为新文本。

我用谷歌搜索并发现了一个相关问题https://github.com/angular/zone.js/issues/137。也就是说,cordova-plugin-file 中的 FileReader 没有分区。但我在我的项目中使用“ionic-angular”:“2.0.0-beta.6”。它不应该已经包含修复程序。我仍然在我的 ionic2 项目中看到相同的问题 137。

代码位于https://github.com/CharlesJu1/FileReaderNotZoned

这是 hello-ionic.ts 文件。

import {Page} from 'ionic-angular';

declare var window: any;
declare var LocalFileSystem: any;

@Page({
  templateUrl: 'build/pages/hello-ionic/hello-ionic.html'
})
export class HelloIonicPage {

  showText: string = 'Initial text string';

  writeText() {
    var newText = 'This is new text';
    function overWriteFile(fileEntry, dataObj) {
      // Create a FileWriter object for our FileEntry.
      fileEntry.createWriter(function(fileWriter) {

        fileWriter.onwriteend = function() {
          fileWriter.seek(0);
          fileWriter.write(dataObj);
        }

        //truncate() will call onwriteend.
        fileWriter.truncate(0);
      });
    }
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
      console.log('file system open: ' + fs.name);
      fs.root.getFile("newPersistentFile.txt", { create: true, exclusive: false }, function(fileEntry) {
        console.log("fileEntry is file?" + fileEntry.isFile.toString());
        overWriteFile(fileEntry, newText);
      }, function(error) { });
    }, function(error) { });
  }

  test() {
    var that = this;
    var update = function() {
      that.showText = 'This is test text not from local file';
    }

    setTimeout(update, 1000);
  }

  readText() {
    var that = this;  
    var readFile = function(fileEntry, readFileCb) {

      fileEntry.file(function(file) {
        var reader = new FileReader();
        reader.onloadend = function() {

          //this in the following points to fileEntry.file
          console.log("Successful file read: " + this.result);
          readFileCb(this.result);
        };

        reader.readAsText(file);
      }, () => { console.log('Error reading file ') });
    }

    var readFileCb = function(fileContent: string) {
        that.showText = fileContent;
        console.log('readFileCb: ' + that.showText);
    }

    //check https://github.com/apache/cordova-plugin-file for details
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {

      console.log('file system open: ' + fs.name);
      fs.root.getFile("newPersistentFile.txt", { create: false }, function(fileEntry) {

        console.log("fileEntry is file?" + fileEntry.isFile.toString());
        // fileEntry.name == 'someFile.txt'
        // fileEntry.fullPath == '/someFile.txt'
        readFile(fileEntry, readFileCb);
      }, function(error) { });
    }, function(error) { });
  }
}

这是 hello-ionic.html 文件。

<ion-navbar *navbar>
  <button menuToggle>
    <ion-icon name="menu"></ion-icon>
  </button>
  <ion-title>Hello Ionic</ion-title>
</ion-navbar>


<ion-content padding class="getting-started">
  <p> 
    {{showText}}
  </p>

  <button (click)="writeText()">
  Write to local file using cordova-plugin-file
</button>

<br>
<button (click)="readText()">
  Read from file using cordova-plugin-file
</button>

<button (click)="test()">
  Set the text with a timer
</button>


</ion-content>
4

1 回答 1

1

区域周围的 beta-5 和 beta-6 存在一些已知问题。一个新的测试版应该很快就会解决这些问题,但与此同时,您可以执行以下步骤:

  1. 导入 NgZone
  2. 注入 NgZone 实例
  3. 在 NgZone 实例的 run 方法中,在您希望数据绑定起作用的地方包装调用。

在此处查看示例:

https://github.com/danbucholtz/ionic2-weight-tracker/blob/master/app/pages/photo-list/PhotoList.ts#L78

谢谢,丹

于 2016-05-06T03:19:01.473 回答