【问题标题】:How to make HTTP requests in AngularJS 2.0 [closed]如何在 AngularJS 2.0 中发出 HTTP 请求 [关闭]
【发布时间】:2015-05-18 18:03:23
【问题描述】:

我只想知道如何在新的 angularjs 2.0 中发出 http 请求,以及如何将此服务连接到控制器。

谢谢。

【问题讨论】:

    标签: javascript http angular


    【解决方案1】:

    到目前为止,http 模块/组件仍在开发中,尚未发布。在此期间,有两种选择:

    您可以在此处了解有关新 http 服务的更多信息:

    【讨论】:

      【解决方案2】:

      我认为已经添加了基本支持。基于此example:

      1. 使用来自 'angular2/http' //index.ts 的 httpInjectables 引导您的应用程序

      2. 在构造函数中注入Http构造器(http:Http) //http_comp.ts

      3. http.get('./people.json').map(res => res.json()).subscribe(people => this.people = people);

      【讨论】:

        【解决方案3】:

        摘录如下:

        import {Component, View} from 'angular2/core';
        import {Http, HTTP_BINDINGS} from 'angular2/http';
        import {bootstrap} 'angular2/platform/browser';
        import {CORE_DIRECTIVES,NgFor} from 'angular/common';
        
            @Component({
               selector: 'app'
            })
        
            @View({
                templateUrl: 'devices.html',
                directives: [CORE_DIRECTIVES,NgFor]
            })
        
            export class App {
                devices: any;
                constructor(http: Http) {
                    this.devices = []; 
                    http.get('./devices.json').toRx().subscribe(res => this.devices = res.json());
                }
            }
        
            bootstrap(App,[HTTP_BINDINGS]);
        

        这是一个演示 angular2 http 功能的存储库,可能会派上用场:

        https://github.com/kaalpurush/ng2-http

        【讨论】:

          猜你喜欢
          • 2020-10-05
          • 2013-03-20
          • 2016-04-30
          • 1970-01-01
          • 2017-08-03
          • 2011-12-19
          • 1970-01-01
          • 1970-01-01
          • 2019-11-19
          相关资源
          最近更新 更多