如何通过Javascript函数在Angular 2中实现AJAX
问题描述:
我想在Angular 2中实现AJAX,但我现在不怎么做。如何通过Javascript函数在Angular 2中实现AJAX
我有我的角2部分:
import { Component, OnInit, AfterViewInit } from '@angular/core';
declare var filter: any;
declare var pageLoaded: any;
@Component({
moduleId: module.id,
selector: 'Summary',
templateUrl: '/app/summary-view/summary.component.html',
styleUrls: [
'./summary.component.css',
]
})
export class Summary implements AfterViewInit {
ngAfterViewInit() {
pageLoaded();
filter();
}
}
JavaScript文件我具备的功能pageLoaded,该功能包括AJAX调用:
function pageLoaded() {
function fillFormattedDates(dates) {
var optionsd = "";
for (var i = 0; i < dates.length; i++) {
if (i == 0) {
optionsd += `<option id=${dates[i]} value=${dates[i]} selected>${formatDate(dates[i])}</option>`;
} else {
optionsd += `<option id=${dates[i]} value=${dates[i]}>${formatDate(dates[i])}</option>`;
}
}
document.getElementById('tableDates').innerHTML = optionsd;
}
$.ajax({
dataType: 'json',
url: `http://${host}${port}/api/v1/chart/c3/dates`,
type: "GET",
cache: false,
success: function(dates) {
fillFormattedDates(dates.data);
refreshTable();
}
});
}
我如何使用实现这个AJAX调用Angular 2?我会很感激你的帮助。
使用'http'模块? https://angular.io/docs/ts/latest/guide/server-communication.html – corn3lius