问)如何将以下observable转换为promise,以便我可以调用它.then(...)?
我希望转换为承诺的方法:
this._APIService.getAssetTypes().subscribe(
assettypes => {
this._LocalStorageService.setAssetTypes(assettypes);
},
err => {
this._LogService.error(JSON.stringify(err))
},
() => {}
);
Run Code Online (Sandbox Code Playgroud)
它调用的服务方法:
getAssetTypes() {
var method = "assettype";
var url = this.apiBaseUrl + method;
return this._http.get(url, {})
.map(res => <AssetType[]>res.json())
.map((assettypes) => {
assettypes.forEach((assettypes) => {
// do anything here you might need....
});
return assettypes;
});
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
目前,在JavaScript中处理一系列异步结果的唯一稳定方法是使用事件系统.但是,正在开发三种替代方案:
流:https
:
//streams.spec.whatwg.org Observables:https:
//tc39.github.io/proposal-observable Async Iterators:https://tc39.github.io/proposal-async-iteration
每个事件和其他事件的差异和好处是什么?
这些中的任何一个是否打算取代事件?
我根本不明白其目的mergeMap.我听过两个"解释:
merge和map" - nope的组合(或者我不能复制这个).请考虑以下代码:
var obs1 = new Rx.Observable.interval(1000);
var obs2 = new Rx.Observable.interval(1000);
//Just a merge and a map, works fine
obs1.merge(obs2).map(x=> x+'a').subscribe(
next => console.log(next)
)
//Who know what - seems to do the same thing as a plain map on 1 observable
obs1.mergeMap(val => Rx.Observable.of(val + `B`))
.subscribe(
next => console.log(next)
)
Run Code Online (Sandbox Code Playgroud)
标有"谁知道什么"的最后一篇文章只不过是一张地图obs1- 重点是什么?
什么是mergeMap真正做到?什么是有效用例的示例?(最好带一些代码)
在我的Angular 2应用程序中,我收到一个错误:
无法读取undefined的属性'title'.
这是一个非常简单的组件,只是试图在这里工作.它击中我的API控制器(奇怪地多次),它似乎在返回一个对象后点击回调.我的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)
Run Code Online (Sandbox Code Playgroud)
该服务(about.service.ts):
import {Http} from 'angular2/http';
import {Injectable} from 'angular2/core';
import {AboutModel} from './about.model';
import 'rxjs/add/operator/map';
@Injectable()
export class AboutService {
constructor(private _http: Http) { }
get() {
return this._http.get('/api/about').map(res => {
console.log(res.json()); // I get …Run Code Online (Sandbox Code Playgroud) 我想知道在绑定时是否有可能使用knockoutjs来传递参数.
我绑定了一个复选框列表,并希望绑定到我的viewmodel中的单个计算observable.在我的viewmodel中(基于传递给read函数的参数)我想根据某些条件返回true/false.
var myViewModel=function(){
this.myprop=ko.computed({read: function(){
//would like to receive an argument here to do my logic and return based on argument.
}
});
};
<input type="checkbox" data-bind="checked: myprop(someval1)" />
<input type="checkbox" data-bind="checked: myprop(someval2)" />
<input type="checkbox" data-bind="checked: myprop(someval3)" />
Run Code Online (Sandbox Code Playgroud)
有什么建议?
我已经开始学习Knockout了,我在按钮点击过滤可观察数组并显示结果时遇到了一些麻烦.
这是我的模特:
function Product(data) {
this.id = data.id;
this.name = data.name;
this.price = data.price;
this.description = data.desc;
this.image = data.image;
this.genre = data.genre;
this.show = data.show;
this.offer_desc = data.offer_desc;
this.offer_id = data.offer_id;
}
function ProductModel() {
var self = this;
self.products = ko.observableArray([]);
$.getJSON('../PHP/Utilities.php?json=true', function(json) {
var mappedProducts = $.map(json, function(item) { return new Product(item) });
self.products(mappedProducts);
});
self.filterProducts = ko.computed(function(genre) {
if(typeof genre === 'undefined') {
return self.products(); //initial load when no genre filter is specified
} else { …Run Code Online (Sandbox Code Playgroud) 我是rxjava的新手.我需要组合两个发出不同类型对象的observable.喜欢的东西Observable<Milk>,并Observable<Cereals>和获得Observable<CerealsWithMilk>.对于像这样的事情,我找不到任何操作员.做这样的事情的rx方式是什么?请注意,Milk并且Cereals是异步的.
在Observables forkJoin文档中,它说args可以是一个数组,但它没有列出这样做的例子:
https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/forkjoin.md
我尝试过类似于我列出的功能(如下所示)但是想出了一个错误:
:3000/angular2/src/platform/browser/browser_adapter.js:76
EXCEPTION: TypeError: Observable_1.Observable.forkJoin is not a function
Run Code Online (Sandbox Code Playgroud)
我的功能的剪切版本如下:
processStuff( inputObject ) {
let _self = this;
return new Observable(function(observer) {
let observableBatch = [];
inputObject.forEach(function(componentarray, key) {
observableBatch.push(_self.http.get(key + '.json').map((res: Response) => res.json()));
});
Observable.forkJoin(
observableBatch
// );
).subscribe(() => {
observer.next();
observer.complete();
});
});
}
Run Code Online (Sandbox Code Playgroud)
我的问题的根与循环结束有关,然后按此处的要求继续:Angular2 Observable - 如何在循环之前等待循环中的所有函数调用结束?
但我还没有完全掌握forkJoin与数组的正确用法以及正确的语法.
我非常感谢您提供的帮助.
thirdFunction() {
let _self = this;
return Observable.create((observer) => {
// return new Observable(function(observer) {
...
observer.next(responseargs);
observer.complete();
});
}
processStuff(inputObject) …Run Code Online (Sandbox Code Playgroud) 我正在使用Angular 2 wit rxjs observable.我创建了这个界面:
interface IGame {
name: string;
description: string;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用它作为一个observable并将其作为输入传递给ui:
@Input() public game: Observable<IGame>;
Run Code Online (Sandbox Code Playgroud)
问题是当在UI中绑定整个对象时,我可以看到它的值被打印并显示:
<h3>{{game | json}}</h3>
Run Code Online (Sandbox Code Playgroud)
虽然在屏幕上使用绑定特定属性(当然是游戏的一部分) - 只是一个空字符串:
<h3>{{game.name}}</h3>
<h3>{{game.description}}</h3>
Run Code Online (Sandbox Code Playgroud)
它有可能吗?我应该将名称和描述作为不同的输入传递吗?
我需要你的帮助,我正试图从我的火力显示器中显示一些数据,但它却给我一个错误InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe'.
有我的服务:
import { Injectable } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
@Injectable()
export class MoviesService {
constructor(private db: AngularFireDatabase) {}
get = () => this.db.list('/movies');
}
Run Code Online (Sandbox Code Playgroud)
有我的组件:
import { Component, OnInit } from '@angular/core';
import { MoviesService } from './movies.service';
@Component({
selector: 'app-movies',
templateUrl: './movies.component.html',
styleUrls: ['./movies.component.css']
})
export class MoviesComponent implements OnInit {
movies: any[];
constructor(private moviesDb: MoviesService) { }
ngOnInit() {
this.moviesDb.get().subscribe((snaps) => {
snaps.forEach((snap) => {
this.movies …Run Code Online (Sandbox Code Playgroud) observable ×10
angular ×6
javascript ×3
rxjs ×3
asynchronous ×2
knockout.js ×2
angularfire2 ×1
binding ×1
dom-events ×1
filter ×1
fork-join ×1
iterator ×1
pipe ×1
promise ×1
pug ×1
rx-android ×1
rx-java ×1
stream ×1
typescript ×1