2016-11-07 36 views
-1

我想在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?我會很感激你的幫助。

+3

使用'http'模塊? https://angular.io/docs/ts/latest/guide/server-communication.html – corn3lius

回答

1

您應該使用Angular2 Http客戶端。官方文檔 - HTTP CLIENT

相關問題