javascript - Angular 替代 $http

标签 javascript ajax angular angular-http

在 AngularJS 中,我使用内置的 $http 服务发送请求。

我应该使用什么来向 Angular 中的服务器发送请求?我找不到任何涵盖该主题的文档。

最佳答案

编辑:有一个api preview对于 Angular 2 上的新 http 服务 website现在

有一个基本的http service目前在 Angular 2 中,但它现在非常简约。该软件处于 alpha 阶段,很可能会发生变化,因此您可能只想使用 fetch API , 实现你自己的 XMLHttpRequest ,或使用类似 jQuery 的库反而。目前,Angular 2 http api 与 fetch API 基本相同。 .鉴于fetch改变的可能性要小得多,我建议只使用它。

如果您想使用 Angular 2 服务,here是一个例子...

import {bootstrap, Component, View, NgFor, Inject} from 'angular2/angular2';
import {Http, httpInjectables} from 'angular2/http';

@Component({selector: 'http-app'})
@View({
  directives: [NgFor],
  template: `
    <h1>people</h1>
    <ul class="people">
      <li *ng-for="#person of people">
        hello, {{person.name}}
      </li>
    </ul>
  `
})
export class HttpCmp {
  people: Object;
  constructor(http: Http) {
    http.get('./people.json').toRx().map(res => res.json()).subscribe(people => this.people = people);
  }
}

关于javascript - Angular 替代 $http,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29361027/

相关文章:

javascript - IE8 中的 jQuery 动态表单创建无法处理大量数据

javascript - AngularJS ng-click 无法正常工作

javascript - 单击带有事件监听器和父节点的主体关闭菜单

javascript - 对 php 的 Ajax 请求在本地主机上工作,但不在实时服务器上工作

javascript - 如何使用 javascript 或 ajax 在其他按钮上使用 onclick 更改按钮的颜色

javascript - AngularJs 中的图像路径

javascript - ajax请求中的表单数据不完整

asp.net - 使用 Angular 从 webapi 检索单个值

javascript - 在 Angular 7 中获取参数值的不同结果

node.js - heroku 中的 "HMR is not enabled for webpack-dev-server "错误