本文整理汇总了TypeScript中rxjs/Rx.Observable.fromArray方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Observable.fromArray方法的具体用法?TypeScript Observable.fromArray怎么用?TypeScript Observable.fromArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rxjs/Rx.Observable
的用法示例。
在下文中一共展示了Observable.fromArray方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: constructor2
// fromArray pattern
constructor2() {
console.log(new Observable());
var startDates = [];
var startDate = new Date();
for (var day = -2; day <=2; day++) {
var date = new Date(
startDate.getFullYear(),
startDate.getMonth(),
startDate.getDate() + day);
startDates.push(date);
}
//console.log(startDates);
var d = Observable.fromArray(startDates)
.map(date => {
console.log("Getting deals for date" + date);
return [1, 2, 3];
});
d.subscribe(x => console.log(x));
}
示例2: beforeEach
beforeEach(inject(function(phones, $rootScope, $controller) {
spyOn(phones, 'query').and.returnValue(Observable.fromArray([
[{name: 'Nexus S'}, {name: 'Motorola DROID'}]
]));
scope = $rootScope.$new();
ctrl = $controller('PhoneListCtrl', {$scope: scope});
}));
示例3: it
it('should fetch phone detail', function() {
spyOn(phones, 'get').and.returnValue(Observable.fromArray([xyzPhoneData()]));
let ctrl = $controller('PhoneDetailCtrl', {$scope: scope});
expect(phones.get).toHaveBeenCalledWith('xyz');
expect(ctrl.phone).toEqual(xyzPhoneData());
});
示例4: constructor3
constructor3() {
console.log(new Observable());
var d1 = Observable.empty();
d1.subscribe(x => console.log(x));
var d2 = Observable.range(1, 5);
d2.subscribe(x => console.log(x));
var d3 = Observable.fromArray([1,2,3]);
d3.subscribe(x => console.log(x));
var d4 = Observable.of(1);
d4.subscribe(x => console.log(x));
var d5 = Observable.of(1,2,3);
d5.subscribe(x => console.log(x));
var d6 = Observable.of([1,2,3]);
d6.subscribe(x => console.log(x));
}
示例5: query
query():Observable<Phone[]> {
return Observable.fromArray([
[{name: 'Nexus S'}, {name: 'Motorola DROID'}]
])
}
示例6: get
get(id):Observable<Phone> {
return Observable.fromArray([xyzPhoneData()]);
}
示例7: constructor
constructor() {
// Test various forms of Observable.
console.log("fromArray [1,2,3]");
var observable = Observable.fromArray([1, 2, 3])
.subscribe(x => console.log(x));
console.log("of [1,2,3]");
observable = Observable.of([1,2,3])
.subscribe(x => console.log(x));
console.log("of 1,2,3");
observable = Observable.of(1,2,3)
.subscribe(x => console.log(x));
console.log("empty");
observable = Observable.empty()
.subscribe(x => console.log(x));
console.log("range 1,5");
observable = Observable.range(1,5)
.subscribe(x => console.log(x));
console.log("completion function");
observable = Observable.of(1)
.subscribe(
x => console.log(x),
err => console.error(err),
() => console.log("completed")
);
// console.log("interval 1000");
// var interval = Observable.interval(10000)
// .flatMap(x => {
// // call server
// return Observable.of([1,2,3]);
// })
// .subscribe(x => console.log(x));
// Simulating error.
console.log("error handling");
var errObs = Observable
.throw(new Error("ahh!"))
.subscribe(
x => console.log(x), // success
err => console.error(err) // error
);
// simulate a call which fails twice then succeeds.
console.log("error handling with retry");
var counter = 0;
var ajaxCall = Observable.of('url')
.flatMap(() => {
if(++counter < 2)
return Observable.throw(new Error("ahh!"));
return Observable.of([1,2,3]);
})
.retry(3)
.subscribe(x => console.log(x), err => console.error(err));
console.log("error handling with catch");
var catchObs = Observable
.throw(new Error("ahh!"))
.catch(err => {
return Observable.of([1,2,3]);
})
.subscribe(x => console.log(x));
console.log("timeout");
var timeout = Observable
.of([1,2,3])
.delay(5000)
.timeout(100)
.subscribe(x => console.log(x), err => console.error(err));
// join two observables. Returns an array of the results
// [o1.results, o2.results] after both complete. Map to something nicer.
console.log("forkJoin");
var o1 = Observable.of({
userId: 1,
username: 'something'
}).delay(2000);
var o2 = Observable.of([1,2,3]).delay(1500);
Observable
.forkJoin(o1,o2)
.map(data => {
{
return {
user: data[0],
stream: data[1]
};
}
})
.subscribe(x => console.log(x));
}
示例8: function
import { Observable } from 'rxjs/Rx';
Observable.fromArray(['AdriĂ ', 'Jen', 'Sergi']).subscribe(function(data){
console.log(data);
},
function(err){
console.log(err)
}, function(){
console.log('completed')
})
示例9: constructor
constructor(fb:FormBuilder) {
this.formModel = fb.group({
search: []
});
var searchControl = this.formModel.find('search');
searchControl.valueChanges
.debounceTime(400)
.map(str => (str.replace(' ', '-')))
.subscribe(x => console.log(x));
/** travel search */
var startDates = [];
var startDate = new Date();
for (var day = -2; day < 2; day++) {
var date = new Date(
startDate.getFullYear(),
startDate.getMonth(),
startDate.getDate() + day
);
startDates.push(date);
}
Observable
.fromArray(startDates)
.map(date => {
console.log("Getting deals for date: " + date);
return [1, 2, 3];
})
.subscribe(x => console.log(x));
/** interval */
// Observable
// .interval(1000)
// .subscribe(x => console.log(x))
// Observable
// .interval(1000)
// .flatMap(x => {
// console.log("Calling server to get latest news..");
// return Observable.of([1, 2, 3]);
// })
// .subscribe(news => console.log(news));
/** delay */
var userStream = Observable.of({
userId: 1, userName: 'chanu'
}).delay(2000);
/** join */
var tweetsStream = Observable.of([1, 2, 3]).delay(1500);
Observable
.forkJoin(userStream, tweetsStream)
.map(joined => Object({user: joined[0], tweets: joined[1]}))
.subscribe(result => console.log(result));
/** error handling */
var observable = Observable.throw(new Error("Something failed!"));
observable
.subscribe(
x => console.log(x),
error => console.log(error));
/** retry */
var counter = 0;
var ajaxCall = Observable
.of('url')
.flatMap(() => {
if (++counter < 2)
return Observable.throw(new Error("Request failed!"));
return Observable.of([1, 2, 3]);
})
.retry(3)
.subscribe(
x => console.log("Response from retry: " + x),
error => console.log(error)
);
/** catching and continuing */
var remoteDataStream = Observable.throw(new Error("Something is wrong!"));
remoteDataStream.catch(err => {
var localDataStream = Observable.of([1, 2, 3]);
return localDataStream;
})
.subscribe(x => console.log("Catching and continuing: " + x));
/** Completed */
Observable
.throw(new Error("error"))
.subscribe(
x => console.log(x),
error => console.log(error),
() => console.log("Completed"));
Observable
.fromArray([1, 2, 3])
.subscribe(
//.........这里部分代码省略.........