How to use httpResource to fetch a text file and use the fetched data to show in HTML

Discussão em 'Angular' iniciado por Naren Murali, Abril 22, 2025.

  1. Naren Murali

    Naren Murali Guest

    I am experimenting with httpResource, I am trying to fetch an text file using the new httpResource method, but I am not sure how to convert the fetched text file to render in an textarea tag.

    Angular.dev - HttpResource Docs

    I think I need to convert this to a URL.

    @Component({
    selector: 'app-root',
    imports: [FormsModule, CommonModule],
    template: `
    <input [(ngModel)]="hash" type="number"/>
    <hr/>
    @if(textResource.value(); as text) {
    <textarea [value]="text" style="height:85vh;width:90vw;"></textarea>
    }
    `,
    })
    export class App {
    hash = signal<number>(200);
    rs = ResourceStatus;
    textResource = httpResource(
    () =>
    `https://www.w3.org/TR/2003/REC-PNG-20031110/iso_8859-1.txt?hash=${this.hash()}`
    );
    }


    When I try this, I do not get any text generated in the textarea.

    Stackblitz Demo

    Continue reading...

Compartilhe esta Página