2017-03-21 29 views
0

我用html5/angular2安裝了「sb admin 2」儀表板。 此示例與打字稿一起使用。爲了實例化圖表,文件charts.compenent.ts定義類,然後定義圖表屬性和數據如下使用打字稿進行REST呼叫

import { Component, OnInit} from '@angular/core'; 

@Component({ 
    moduleId: module.id, 
    selector: 'chart-cmp', 
    templateUrl: 'chart.component.html' 
}) 

export class ChartComponent implements OnInit { 
    ngOnInit() { 

     var container:any = $('#container'); 
     container.highcharts({ 
      chart: { 
       type: 'area' 
      }, 
................................... 

在我的情況,我想從一個問題的REST服務的日期。

你能幫我做這個嗎? 任何投入將有助於

回答

1

請確保您有正確的進口,

import {Http, Response, URLSearchParams}           from '@angular/http'; 

這是如何使一個GET請求,

Get請求

 saveProfile(model: Profile, isValid: boolean) { 
       let params: URLSearchParams = new URLSearchParams(); 
// set params to go to URL 
       params.set('email', model.email); 
       params.set('first_name', model.first_name); 

       return this.http.get('url/path/here/dont/forget/port', 
        { search: params }) 
        .map((res: Response) => res.json()) 
        .subscribe((res) => { 
         console.log(res); 
    // Map the values in the response to useable variables 
         this.auth.user.email = res.user.email; 
         this.auth.user.first_name = res.user.first_name; 
        }); 
      } 
     } 

帖子要求

如何發佈帖子請求,這是auth0庫中常用的帖子請求。你可以發現here

authenticate(username, password) { 

    let creds = JSON.stringify({ username: username.value, password: password.value }); 

    let headers = new Headers(); 
    headers.append('Content-Type', 'application/json'); 

    this.http.post('http://localhost:3001/sessions/create', creds, { 
    headers: headers 
    }) 
    .subscribe(
     data => { 
     this.saveJwt(data.json().id_token); 
     username.value = null; 
     password.value = null; 
     }, 
     err => this.logError(err.json().message), 
    () => console.log('Authentication Complete') 
    ); 
    } 

這些例子將得到服務器的響應。如果你想做更多技術性的事情,比如在視圖中獲取新的數據來更新,你將不得不創建一個observable。如果我是你,我會得到這個,然後當你需要了解observable你可以合併。