javascript - 使用 Ionic 3 在页面模板内加载资源文件

标签 javascript angular ionic-framework ionic3

我有一个页面 ContentPage,它显示我的应用程序的静态文件的内容。我的页面的 html 文件如下(content.html):

<ion-header>
  <ion-navbar>
    <ion-title>{{ lessonNumber }} - {{ pageNumber }}</ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>

  <!-- TXT CONTENT GOES HERE -->
  {{ txtContent }}

</ion-content>

我的静态文件位于src/assets/txts/

src
|_ app
|_ assets
   |_ txts
      |_ 1.txt
      |_ 2.txt
      |_ 3.txt
      |_ ...
|_ pages
|_ ...

ContentPage打开时,我需要获取其中一个txt文件,例如1.txt,并将其内容加载到content.html中 页面。

这是我的页面代码:

import {Component} from "@angular/core";
import {IonicPage} from "ionic-angular";

@IonicPage()
@Component({
  selector: 'page-content',
  templateUrl: 'content.html',
})
export class ContentPage {

  lessonNumber: number;
  pageNumber: number;

  txtContent: string = '';

  constructor() {
    //FIXME - get txt contents
    //txtContent = ...
  }

}

我怎样才能做到这一点?

是否有类或插件可以让我获取这些文件?

最佳答案

您可以使用HTTP请求从txt文件中获取数据。您可以在构造函数或服务中编写

constructor() {
    this.http.request('./txt/1.txt')
                     .map(res => res.text()    })
          .subscribe(text => {
            this.txtContent= text;
          });
 }

关于javascript - 使用 Ionic 3 在页面模板内加载资源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44610861/

相关文章:

javascript - 在 Sencha Touch 2 中的 itemswipe 上显示复选框?

javascript - 我在 Node.js 异步函数中收到 Promise { <pending> }

php - 当 readyState 为 4 时,为什么我在脚本中调用的 Ajax 函数会连续运行两次?

angular - 如何解决 "unexpected token : A constructor, method, accessor, or property was expected"错误?

javascript - Ionic v2 + CORS 预检访问控制允许方法

android - 为什么 Ionic 默认 ionic 内容的 has-bounce 为 false?

javascript - 同步 Ajax 调用和executeQueryAsync Sh​​arePoint JS CSOM

Angular 延迟加载模块和共享模型类

html - CSS 网格自动流 : dense not working

javascript - ionic : Enable scroll in inset list