“想从txt网址获取文本数据,然后在angular web中获取它。如何从txt URL获取文本?如何从Angular 7中使用Filereader?”
“这是针对Angular 7的。”
<a (click)="getTextFile()">Check</a>
import {Component} from '@angular/core';
import {FormControl} from '@angular/forms';
import {Observable} from 'rxjs';
import {map, startWith} from 'rxjs/operators';
import {
HttpClient,
HttpRequest,
HttpResponse,
HttpParams,
HttpHeaders
} from '@angular/common/http';
@Component({
selector: 'autocomplete-overview-example',
templateUrl: 'autocomplete-overview-example.html',
styleUrls: ['autocomplete-overview-example.css'],
})
export class AutocompleteOverviewExample {
text:any;
constructor(
private http: HttpClient,
) {
}
getTextFile():void{
this.http.get('https://creamson-intelli.slack.com/files/UFL3QQFP0/FL4N4SNFJ/young_ruskin_bond.txt').subscribe((data) => {
this.text=data;
console.log(this.text);
})
}
}
答案 0 :(得分:0)
选中此
service.ts
data RuntimeValue = Value1 | Value2
someFunction :: RuntimeValue -> UnifiedType a
case runtimeValue of
Value1 -> someAction (Proxy :: Proxy 'Value1)
Value2 -> someAction (Proxy :: Proxy 'Value2)
component.ts
getRawData(): Observable<any> {
const api = 'https://www.w3.org/TR/PNG/iso_8859-1.txt';
return this.httpClient.get(api,{ responseType: 'text' });
}
.html
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { AppService } from './app.service';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
name = 'Angular 5';
rawlist: any;
constructor(private appService: AppService) {}
ngOnInit() {}
getRawData() {
this.appService
.getRawData()
.subscribe(
data => this.rawlist=data,
error => console.log(error)
);
}
}