Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript angular 2 http.get subscribe:服务完成时如何调用另一个函数?_Javascript_Angular_Typescript_Observable_Angular2 Services - Fatal编程技术网

Javascript angular 2 http.get subscribe:服务完成时如何调用另一个函数?

Javascript angular 2 http.get subscribe:服务完成时如何调用另一个函数?,javascript,angular,typescript,observable,angular2-services,Javascript,Angular,Typescript,Observable,Angular2 Services,不明白。如果我需要我的结果来做更多的事情,不只是输入我的变量heros例如。我想在成功或完成后调用另一个函数,但我就是不能。这是为什么?应该如何做?我有另一个变量需要获取从响应返回的相同数据(它的副本),但我只能在获取数据后进行复制 this.myService.getHeroes() .subscribe( function(response) { response => this.heros = response; },

不明白。如果我需要我的结果来做更多的事情,不只是输入我的变量heros例如。我想在成功或完成后调用另一个函数,但我就是不能。这是为什么?应该如何做?我有另一个变量需要获取从响应返回的相同数据(它的副本),但我只能在获取数据后进行复制

this.myService.getHeroes()
    .subscribe(
        function(response) {
            response => this.heros = response;
        },
        function(error) {
            console.log("Error happened" + error)
        },
        function() {
            console.log("the subscription is completed");
        }
    );

您可以在收到响应后立即调用该函数

this.myService.getHeroes()
.订阅(res=>{
this.heros=res;
//在此处插入所需内容,例如需要等待asynchro响应的函数
},
错误=>{
日志(“发生错误”+错误)
}

);您可以在收到响应后立即调用该函数

this.myService.getHeroes()
.订阅(res=>{
this.heros=res;
//在此处插入所需内容,例如需要等待asynchro响应的函数
},
错误=>{
日志(“发生错误”+错误)
}

);扩展此类用户提供的内容:

无法访问其他组件变量的原因是
this
关键字的作用域仅封装在函数中,不再了解组件变量

为了引用组件变量,必须使用lambda表达式:

@Component({
  selector: 'app-browse',
  templateUrl: './browse.component.html',
  styleUrls: ['./browse.component.css']
})
export class BrowseComponent implements OnInit {

  constructor(private myService: MyService) { }

  myString: string = 'dogs';

  doStuff() {
    this.myService.doMoreStuff().subscribe(returnValue => {
      console.log(this.myString); // 'dogs'
      this.myOtherFunction(); // 'other stuff'
    });

    this.myService.doMoreStuff().subscribe(function(returnValue) {
      console.log(this.myString); // undefined
      // myString does not exist with the scope of this function
      var myString = 'cats';
      console.log(this.myString); // 'cats'
    });
  }

  myOtherFunction() { console.log('otherStuff'); }

}

要扩展此类用户提供的内容:

无法访问其他组件变量的原因是
this
关键字的作用域仅封装在函数中,不再了解组件变量

为了引用组件变量,必须使用lambda表达式:

@Component({
  selector: 'app-browse',
  templateUrl: './browse.component.html',
  styleUrls: ['./browse.component.css']
})
export class BrowseComponent implements OnInit {

  constructor(private myService: MyService) { }

  myString: string = 'dogs';

  doStuff() {
    this.myService.doMoreStuff().subscribe(returnValue => {
      console.log(this.myString); // 'dogs'
      this.myOtherFunction(); // 'other stuff'
    });

    this.myService.doMoreStuff().subscribe(function(returnValue) {
      console.log(this.myString); // undefined
      // myString does not exist with the scope of this function
      var myString = 'cats';
      console.log(this.myString); // 'cats'
    });
  }

  myOtherFunction() { console.log('otherStuff'); }

}

首先,您的语法是错误的,这将无法传输。您需要
(response)=>this.heros=response
不是
函数
和箭头函数的组合。10x我现在使用它,但仍然是相同的问题。就像我不能从success/error/complete函数中调用任何其他函数一样,首先你必须定义一个函数,然后调用它/:首先,你的语法是错误的,这是不可传输的。您需要
(response)=>this.heros=response
不是
函数
和箭头函数的组合。10x我现在使用它,但仍然是相同的问题。就像我不能从success/error/complete函数中调用任何其他函数一样,首先你必须定义一个函数,然后再调用它/:不,我得到一个错误,你的函数不是function@AngularOne我只是举个例子。。。您可以在那里插入您想要的任何内容,例如某些函数,它们必须等待异步响应。您也可以直接删除它。@AngularOne@Kinduser是对的。您应该在
this.heros=res
之后调用
函数。Observable
complete
Observer
以Observable结束操作时调用函数。但如果没有呢?也许这个流是无限的,那么
complete
现在就不再调用它了。错误之后是什么,另一个函数完成了?@AngularOne在
错误
函数之后是什么?不,我得到一个错误,你的函数不是function@AngularOne我只是举个例子。。。您可以在那里插入您想要的任何内容,例如某些函数,它们必须等待异步响应。您也可以直接删除它。@AngularOne@Kinduser是对的。您应该在
this.heros=res
之后调用
函数。Observable
complete
Observer
以Observable结束操作时调用函数。但如果没有呢?也许这个流是无限的,那么
complete
现在就不再调用它了。错误之后是什么,另一个函数完成了?@AngularOne在
错误之后是我们的函数吗?