Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I want to implement AJAX in Angular 2, but I don't now how to do this.

I have my Angular 2 component:

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();
    }

}

In a Javascript file I have the function pageLoaded, this function includes an AJAX Call:

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();
        }
    });
}

How can I implement this AJAX call using Angular 2? I will really appreciate your help.

share|improve this question
3  

You should use Angular2 Http client. Official doc - HTTP CLIENT

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.