Tag: 可观的

如何创build一个类似于Angular 2中的http的静态数据的Observable?

我有一个服务,有这个方法: export class TestModelService { public testModel: TestModel; constructor( @Inject(Http) public http: Http) { } public fetchModel(uuid: string = undefined): Observable<string> { if(!uuid) { //return Observable of JSON.stringify(new TestModel()); } else { return this.http.get("http://localhost:8080/myapp/api/model/" + uuid) .map(res => res.text()); } } } 在组件的构造函数中,我是这样订阅的: export class MyComponent { testModel: TestModel; testModelService: TestModelService; constructor(@Inject(TestModelService) testModelService) { this.testModelService […]

Observabletypes错误:无法读取undefined的属性

Angular 2应用程序,我得到一个错误:不能读取未定义的属性“标题”。 这是一个非常简单的组件,只是为了获得最低限度的工作。 它命中我的API控制器(好奇地多次),它似乎击中了我的代码中返回对象后的callback区域。 我的console.log输出我期望的对象。 这是完整的错误: TypeError: Cannot read property 'title' of undefined at AbstractChangeDetector.ChangeDetector_About_0.detectChangesInRecordsInternal (eval at <anonymous> (http://localhost:55707/lib/angular2/bundles/angular2.dev.js:10897:14), <anonymous>:31:26) at AbstractChangeDetector.detectChangesInRecords (http://localhost:55707/lib/angular2/bundles/angular2.dev.js:8824:14) at AbstractChangeDetector.runDetectChanges (http://localhost:55707/lib/angular2/bundles/angular2.dev.js:8807:12) at AbstractChangeDetector._detectChangesInViewChildren (http://localhost:55707/lib/angular2/bundles/angular2.dev.js:8877:14) at AbstractChangeDetector.runDetectChanges (http://localhost:55707/lib/angular2/bundles/angular2.dev.js:8811:12) at AbstractChangeDetector._detectChangesContentChildren (http://localhost:55707/lib/angular2/bundles/angular2.dev.js:8871:14) at AbstractChangeDetector.runDetectChanges (http://localhost:55707/lib/angular2/bundles/angular2.dev.js:8808:12) at AbstractChangeDetector._detectChangesInViewChildren (http://localhost:55707/lib/angular2/bundles/angular2.dev.js:8877:14) at AbstractChangeDetector.runDetectChanges (http://localhost:55707/lib/angular2/bundles/angular2.dev.js:8811:12) at AbstractChangeDetector.detectChanges (http://localhost:55707/lib/angular2/bundles/angular2.dev.js:8796:12) 服务(about.service.ts): import {Http} from 'angular2/http'; import {Injectable} from […]

使用TypeScript从Angular2的http数据链接RxJS Observable

我目前正在尝试自学Angular2和TypeScript,在过去的4年中与AngularJS 1. *愉快地合作。 我不得不承认,我讨厌它,但是我确定我的灵感即将到来…无论如何,我已经在我的虚拟应用程序中写了一个服务,它将从我写的服务JSON的虚假后端获取http数据。 import {Injectable} from 'angular2/core'; import {Http, Headers, Response} from 'angular2/http'; import {Observable} from 'rxjs'; @Injectable() export class UserData { constructor(public http: Http) { } getUserStatus(): any { var headers = new Headers(); headers.append('Content-Type', 'application/json'); return this.http.get('/restservice/userstatus', {headers: headers}) .map((data: any) => data.json()) .catch(this.handleError); } getUserInfo(): any { var headers = new […]