Javascript 使用angular2/typescript获取数据
我对angular2和打字脚本有问题。 我尝试获取数据jsonp,但返回函数是可观察的,我需要de对象结果。我需要帮助 会计科目Javascript 使用angular2/typescript获取数据,javascript,angular,typescript,ionic2,Javascript,Angular,Typescript,Ionic2,我对angular2和打字脚本有问题。 我尝试获取数据jsonp,但返回函数是可观察的,我需要de对象结果。我需要帮助 会计科目 import { Component} from '@angular/core'; import { NavController, NavParams } from 'ionic-angular'; import {AccountService} from '../../providers/account-service'; import { Observable
import { Component} from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import {AccountService} from '../../providers/account-service';
import { Observable } from 'rxjs/Rx';
@Component({
selector: 'page-account',
templateUrl: 'account.html',
providers: [AccountService]
})
export class AccountPage {
data: any;
listAccounts: any;
accounts: any;
constructor(public navCtrl: NavController, public navParams: NavParams, private AccountService: AccountService) {
this.accounts = this.getAccountsResult();
}
getAccountsResult() {
return this.AccountService.retrieveAccounts();
}
getAccountsResultDebug() {
console.log(this.AccountService.retrieveAccounts());
}
}
账户服务
import { Injectable } from '@angular/core';
import { Headers, Http, RequestOptions, Jsonp } from '@angular/http';
import 'rxjs/Rx';
/*
Generated class for the AccountService provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class AccountService {
/**
* Variaveis globais
**/
data: Array<AccountService>;
token: string;
url: string;
param: string;
result: any;
constructor(private http: Http, private jsonp: Jsonp) {
this.http = http;
this.jsonp = jsonp;
this.token = 'tokenofapp'
this.url = 'urlofapp'
}
/**
* PROGRAMADOR: Débora Gonçalves
* DATA: 23/01/2017
* OBJETIVO: Recuperando dados da API accounts
**/
retrieveAccounts() {
// variavel para enviar os headers
let headers = new Headers();
//definindo valores no array headers
headers.append('x-access-key', this.token);
headers.append('Content-Type', 'application/jsonp');
// definindo o requestoptions com os parametros do headers
let options = new RequestOptions({
headers: headers
});
//executando requisição da url
//return this.jsonp.get(this.url, options).map(res => res.json());
// .subscribe(
// data => this.result = [data]
//);
return this.jsonp.get(this.url, options).map(res => res.json());
}
}
从'@angular/core'导入{Injectable};
从'@angular/Http'导入{Headers,Http,RequestOptions,Jsonp};
进口“rxjs/Rx”;
/*
为AccountService提供程序生成的类。
看见https://angular.io/docs/ts/latest/guide/dependency-injection.html
有关提供者和Angular 2 DI的更多信息。
*/
@可注射()
出口类会计服务{
/**
*全球变形虫
**/
数据:数组;
令牌:字符串;
url:string;
参数:字符串;
结果:有;
构造函数(私有http:http,私有jsonp:jsonp){
this.http=http;
this.jsonp=jsonp;
this.token='tokenofapp'
this.url='urlofapp'
}
/**
*节目主持人:Débora Gonçalves
*数据:2017年1月23日
*目标:重现API帐户中的数据
**/
检索帐户(){
//variavel para enviar操作系统标头
let headers=新的headers();
//definindo valores无数组头
headers.append('x-access-key',this.token);
headers.append('Content-Type','application/jsonp');
//定义请求选项com os参数do头
让选项=新的请求选项({
标题:标题
});
//要求执行人
//返回this.jsonp.get(this.url,options.map)(res=>res.json());
//.订阅(
//data=>this.result=[data]
//);
返回this.jsonp.get(this.url,options.map)(res=>res.json());
}
}
会计页
<ion-header>
<ion-navbar>
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title>Contas</ion-title>
</ion-navbar>
</ion-header>
<ion-content>
<ion-list>
<button ion-item outline *ngFor="let i of accounts.result" (click)="openPage($event, 1, DetailsAccountPage)">
<p>a</p>{{i}}
<p *ngFor="let f of i" strong>Nome do fulano {{f}}</p>
</button>
<button ion-item outline>
<p strong>Nome do fulano</p>
<div class="item-note" item-right>
</div>
</button>
<button ion-item outline (click)="getAccountsResultDebug()">
<p strong>Nome do fulano</p>
<div class="item-note" item-right>
</div>
</button>
</ion-list>
</ion-content>
康塔斯
a{i}
Nome do fulano{{f}
Nome do fulano
Nome do fulano
您需要在component.ts中订阅Angular 2服务
跟随
这需要在您的component.ts中
无论你在哪里呼唤你的服务。
.订阅(
data=>this.result=[data]
); 你能告诉我发生了什么错误吗?或是任何工作的东西。