2017-03-21 63 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你可以合并。