如何调用具有文本文件的 URL,将其转换为字符串并在 ionic 2 中显示?
How do I call a URL that has a text file, convert it to a string and display it in ionic 2?
我想要做的就是调用一个具有文本格式数据的 URL,将其转换为字符串变量并在我的 ionc 2 应用程序中将其作为消息或 toast 输出。
到目前为止,我的应用程序显示了该按钮,但是当我单击它以激活 setMessage() 函数时,它根本没有显示任何内容。
到目前为止,我的代码如下所示。
这是我的about.html
<ion-header>
<ion-navbar primary>
<ion-title>
Sismos
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding class="about">
<button (click)="setMessage()"> Button</button>
</ion-content>
这是我的 about.ts 文件
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {Http} from '@angular/http';
import 'rxjs/add/operator/map';
import { Injectable } from '@angular/core';
//La libreria de abajo la importé para conectarme al Catalogo de Sismos
// el link es http://www.prsn.uprm.edu/Data/prsn/EarlyWarning/Catalogue.txt
//import {Http} from '@angular/http';
@Component({
templateUrl: 'build/pages/about/about.html'
})
export class AboutPage {
message: any;
// items: any;
constructor(public navCtrl: NavController, private http: Http)
{
// this.items = [];
}
setMessage() {
let URL_Data = this.http.get('http://www.prsn.uprm.edu/Data/prsn/EarlyWarning/Catalogue.txt');
let message = URL_Data.toString();
return message;
}
}
您正在返回一个字符串,但没有对其进行任何操作。您正在设置 message
但未在您的页面上显示它。
尝试在 html f.e 中设置一个变量。 <p>{{message}}</p>
(如果使用这个不要忘记初始化您的消息,message:string = "no message available";
)
在您的 .ts 文件中
...//imports, component decorator
export class AboutPage{
message:string = "no message yet";
..... //constructor etc.
setMessage(){
this.http.get("url").subscribe(response => {
this.message = response.toString();
//asynchronous method converting the returned Observable
//(http.get returns Observable) and setting it to the message property.
});
//note that, code here will be executed BEFORE the subscribe method (asynchronous)
//so make sure everything you need to do with the message or response is in the subscribe()
}
}
我想要做的就是调用一个具有文本格式数据的 URL,将其转换为字符串变量并在我的 ionc 2 应用程序中将其作为消息或 toast 输出。 到目前为止,我的应用程序显示了该按钮,但是当我单击它以激活 setMessage() 函数时,它根本没有显示任何内容。
到目前为止,我的代码如下所示。
这是我的about.html
<ion-header>
<ion-navbar primary>
<ion-title>
Sismos
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding class="about">
<button (click)="setMessage()"> Button</button>
</ion-content>
这是我的 about.ts 文件
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {Http} from '@angular/http';
import 'rxjs/add/operator/map';
import { Injectable } from '@angular/core';
//La libreria de abajo la importé para conectarme al Catalogo de Sismos
// el link es http://www.prsn.uprm.edu/Data/prsn/EarlyWarning/Catalogue.txt
//import {Http} from '@angular/http';
@Component({
templateUrl: 'build/pages/about/about.html'
})
export class AboutPage {
message: any;
// items: any;
constructor(public navCtrl: NavController, private http: Http)
{
// this.items = [];
}
setMessage() {
let URL_Data = this.http.get('http://www.prsn.uprm.edu/Data/prsn/EarlyWarning/Catalogue.txt');
let message = URL_Data.toString();
return message;
}
}
您正在返回一个字符串,但没有对其进行任何操作。您正在设置 message
但未在您的页面上显示它。
尝试在 html f.e 中设置一个变量。 <p>{{message}}</p>
(如果使用这个不要忘记初始化您的消息,message:string = "no message available";
)
在您的 .ts 文件中
...//imports, component decorator
export class AboutPage{
message:string = "no message yet";
..... //constructor etc.
setMessage(){
this.http.get("url").subscribe(response => {
this.message = response.toString();
//asynchronous method converting the returned Observable
//(http.get returns Observable) and setting it to the message property.
});
//note that, code here will be executed BEFORE the subscribe method (asynchronous)
//so make sure everything you need to do with the message or response is in the subscribe()
}
}