用angular请求ajax,如何使用angular2 / http进行ajax调用?

我正在使用Angular 2.0.0-beta.0和typescript.我能够在没有任何问题的情况下使用window [‘fetch’]开启ajax调用(我不需要使用早期版本中所需的任何变通方法.)

但我无法使用angular2的http进行相同的ajax调用.

以下是演示此问题的代码.项目文件夹中有三个文件index.html,以及名为app的子文件夹中的boot.ts和app.component.ts.

app.component.ts:

import {Http,HTTP_PROVIDERS} from 'angular2/http';

import {Component} from 'angular2/core';

@Component({

selector: 'my-app',providers: [HTTP_PROVIDERS],template: '

My First Angular 2 App

click{{tasks}}'

})

export class AppComponent {

http: Http;

tasks = [];

constructor(http: Http) {

alert(http == null);

this.http = http;

}

getTasks(): void {

this.http.get('http://localhost:3000/tasks/norender');

//.map((res: Response) => res.json())

//it is important to have this subscribe call,since the

//request is only made if there is a subscriber

.subscribe(res => this.tasks = res);

}

}

boot.ts:

import {bootstrap} from 'angular2/platform/browser'

import {HTTP_PROVIDERS} from 'angular2/http';

import {AppComponent} from './app.component'

bootstrap(AppComponent,[HTTP_PROVIDERS]);

index.html的:

Angular 2 QuickStart

System.config({

transpiler: 'typescript',typescriptOptions: {emitDecoratorMetadata: true},packages: {'app': {defaultExtension: 'ts'}}

});

System.import('app/boot')

.then(null,console.error.bind(console));

Loading...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值