从另一个中的一个可观察者获得价值

时间:2017-04-20 12:03:24

标签: angular rxjs observable

我有SELECT DATEDIFF(SELECT DATE_FORMAT(SYSDATE(),'%Y-%m-%d'), (SELECT birth_date FROM salaries as s, employees as e WHERE salary = (SELECT MAX(salary) FROM salaries) and s.emp_no = e.emp_no)/365.25); 调用function1()来返回一个Observable。

功能1

function2()

function1(url: string):Observable<boolean> { return this.function2().map(res=>{ if (this.data== true) { return true; } else{ this.router.navigate(['/error-user']); return false; } }); 又调用了另一个函数function2()

函数2

function3()

function2(){ debugger; return this.service.getSettings() .do( response => { //some data this.function3(); //this.data from function3() should be accesible here }); } 订阅服务电话。

功能3

function3()

现在我希望function3(){ this.service.getUserInfo () .subscribe( response => { //some data this.data=true; }) } this.data中的function3()可以在function2()中访问,因为我正在使用function1()运算符在map中使用它。

到目前为止,我所尝试的内容如下:

function2(){
    debugger;
    return this.service.getSettings()
        .do(

                  response => {

                      //some data

                      this.function3().map(
                      ()=> console.log( this.data) ).subscribe(()=>{return this.data}); 
                      //this.data from function3() should be accesible here                                                       
                  }); 

  }

function3(){

            return this.service.getUserInfo ()
                            .do(
                                response => {                         
                                    //some data
                                  this.data=true; 
                                })

            }

但是如上所述,我无法在function2()的范围内从function3()获取this.data的值。另外this.data也应该传递给function1()。

1 个答案:

答案 0 :(得分:0)

Observable也使用function3()

function3():Observable<any> {
    // do stuff and get data
    return data;
  }

然后订阅它,您需要在其中调用它并访问它的数据:

    this.function3().subscribe((data) => {
      let myData = data; // for using it inside this block
      console.log(myData);
    });

更新1:

function2(){
    let myData = {}; // if data an obj
    debugger;
    return this.service.getSettings()
        .do(

                  response => {

                      //some data

                      this.myData = this.function3().subscribe((data) => {
                         return data;
                      });
                      // the data from function3() is now available here                                                       
                  }); 

  }

更新2:

更好理解的另一个例子。这就是我使用它的方式:

功能:

   getLoginInfo(): Observable<any> {
        return this.jsonApiService.fetch('/user/login-info.json')
            .do((user) => {
                this.userInfo = user;
                this.user.next(user);
            });
    }

这是我称之为获取数据的地方:

    private user: any;

    constructor(private userService: UserService) {
    }

    ngOnInit() {
        this.userService.getLoginInfo().subscribe(user => {
            this.user = user;
        });
    }