如何使用angular2(ts)进行AJAX调用?
我在angularjs.org上阅读了该教程。但是关于AJAX却一无所获。
因此,我真的很想知道如何使用angular2(ts)进行AJAX调用。

最佳答案

您将要查看api docs for the http modulehttp类可以使用AJAX为您获取资源。有关更多示例,请参见Angular HttpClient Guide

import { Component } from '@angular/core';
import { Http } from '@angular/http';

@Component({
  selector: 'http-app',
  templateUrl: 'people.html'
})
class PeopleComponent {
  constructor(http: Http) {
    http.get('people.json')
      // Call map on the response observable to get the parsed people object
      .map(res => res.json())
      // Subscribe to the observable to get the parsed people object and attach it to the
      // component
      .subscribe(people => this.people = people);
  }
}

07-28 05:29
查看更多