【问题标题】:How to Implement AJAX in Angular 2 through a Javascript Function如何通过 Javascript 函数在 Angular 2 中实现 AJAX
【发布时间】:2016-11-07 19:08:56
【问题描述】:

我想在 Angular 2 中实现 AJAX,但我现在不知道怎么做。

我有我的 Angular 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();
        }
    });
}

如何使用 Angular 2 实现这个 AJAX 调用?非常感谢您的帮助。

【问题讨论】:

标签: javascript angularjs ajax function angular


【解决方案1】:

您应该使用 Angular2 Http 客户端。官方文档-HTTP CLIENT

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    • 2012-01-07
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    相关资源
    最近更新 更多