当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Observable.fromArray方法代码示例

本文整理汇总了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));        
        
        
    }    
开发者ID:kwanling,项目名称:ng2test,代码行数:27,代码来源:app3.component.ts

示例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});
 }));
开发者ID:BaronVonPerko,项目名称:angular.io,代码行数:7,代码来源:phone_list.controller.spec.ts

示例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());
  });
开发者ID:BaronVonPerko,项目名称:angular.io,代码行数:8,代码来源:phone_detail.controller.spec.ts

示例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));
                
    }
开发者ID:kwanling,项目名称:ng2test,代码行数:22,代码来源:app3.component.ts

示例5: query

 query():Observable<Phone[]> {
   return Observable.fromArray([
     [{name: 'Nexus S'}, {name: 'Motorola DROID'}]
   ])
 }
开发者ID:BaronVonPerko,项目名称:angular.io,代码行数:5,代码来源:phone_list.component.spec.ts

示例6: get

 get(id):Observable<Phone> {
   return Observable.fromArray([xyzPhoneData()]);
 }
开发者ID:BaronVonPerko,项目名称:angular.io,代码行数:3,代码来源:phone_detail.component.spec.ts

示例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));
    }
开发者ID:lee-woodridge,项目名称:angular2-course,代码行数:96,代码来源:app.component.ts

示例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')
})
开发者ID:blackhawk389,项目名称:ReactiveProgrammingPractising-,代码行数:10,代码来源:app4.ts

示例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(
//.........这里部分代码省略.........
开发者ID:chanusukarno,项目名称:Angular2-Examples,代码行数:101,代码来源:observables.component.ts


注:本文中的rxjs/Rx.Observable.fromArray方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。