小编rya*_*ain的帖子

如何使用Angular的2+ @ angular/http模块接收blob响应?

我正试图从一个有角度的2应用程序中提供PDF文件下载...

这段代码有效:

    var reportPost = 'variable=lsdkjf';

    var xhr = new XMLHttpRequest();

    xhr.open("POST", "http://localhost/a2/pdf.php", true);
    xhr.responseType = 'blob';
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    xhr.onreadystatechange = function() {//Call a function when the state changes.
        if(xhr.readyState == 4 && xhr.status == 200) {
            var blob = new Blob([this.response], {type: 'application/pdf'});
            saveAs(blob, "Report.pdf");
        }
    }

    xhr.send(reportPost);
Run Code Online (Sandbox Code Playgroud)

但我本来希望使用angular 2的内置Http客户端.

一点研究:

和一些测试代码:

    var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');

    this.http.post('http://localhost/a2/pdf.php', reportPost,  {
        headers: headers
        })
        .retry(3)
        // .map( (res:any) => …
Run Code Online (Sandbox Code Playgroud)

rxjs angular

27
推荐指数
2
解决办法
6万
查看次数

如何在TypeScript中正确更改变量的类型?

感谢您的耐心等待,我刚开始使用TypeScript.

我正在开发一个角度2的应用程序,需要接受文本输入,然后进行一堆计算.我(错误地?)假设我需要首先将输入绑定到我的数据模型中的"任何"类型变量,然后将这些变量转换为数字以便处理数字.我环顾四周,无法找到如何做到这一点,它不会抛出这个TS编译器错误:

`src/calculator_service.ts(40,5): error TS2322: Type 'number' is not assignable to type 'string'.`
Run Code Online (Sandbox Code Playgroud)

在我的CalculatorService中,我有这个功能:

/*
 * Convert the object of strings recieved from the form into a clean object of integers
 */
n(model:ModelFields) {
    // Clone it
    this.numericModel = Object.assign({}, this.model);

    for (var prop in this.numericModel) {
        if (this.numericModel.hasOwnProperty(prop)) {

            // strip off all non-numeric charactersklj
            this.numericModel[prop] = this.numericModel[prop].replace(/\D/g,'');

            // convert to Any typescript type
            // this breaks the application, and still throws a compiler error. nope.
            // this.numericModel[prop] = …
Run Code Online (Sandbox Code Playgroud)

typescript typescript1.5 angular

10
推荐指数
1
解决办法
2万
查看次数

标签 统计

angular ×2

rxjs ×1

typescript ×1

typescript1.5 ×1