javascript - angular 2 http.get subscribe: How to call another function when service completed? - Stack Overflow

Don't get it. If I need my result to do something more, not just enter my variable heros for examp

Don't get it. If I need my result to do something more, not just enter my variable heros for example. I want to call another function on success or pleted but I just can't. Why is that and how it should be done? I have another variable need to be getting the same data returned from the response (copy of it), but I can make the copy only after I get the data.

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

Don't get it. If I need my result to do something more, not just enter my variable heros for example. I want to call another function on success or pleted but I just can't. Why is that and how it should be done? I have another variable need to be getting the same data returned from the response (copy of it), but I can make the copy only after I get the data.

this.myService.getHeroes()
    .subscribe(
        function(response) {
            response => this.heros = response;
        },
        function(error) {
            console.log("Error happened" + error)
        },
        function() {
            console.log("the subscription is pleted");
        }
    );
Share Improve this question edited Feb 24, 2017 at 23:03 kind user 42k8 gold badges68 silver badges78 bronze badges asked Feb 24, 2017 at 21:19 AngularOneAngularOne 2,8806 gold badges35 silver badges46 bronze badges 3
  • To begin with your syntax is wrong, that will not transpile. You want (response) => this.heros = response; and not a bination of function and the arrow function. – Igor Commented Feb 24, 2017 at 21:21
  • 10x I currect it but still the same issue. like I can not call any other function from inside the success/error/plete functions – AngularOne Commented Feb 24, 2017 at 21:25
  • First you have to define a function then call it/: – Roman C Commented Feb 24, 2017 at 22:01
Add a ment  | 

2 Answers 2

Reset to default 4

You can call the function just after you get the response.

this.myService.getHeroes()
   .subscribe(res => {
      this.heros = res;
      //insert whatever you want here, e.g. function which needs to wait for asynchro response
    },
    error => { 
      console.log("Error happened" + error)
    }
 );

To expand upon what such a Kind user provided:

The reason you are unable to access your other ponent variables is because the this keyword's scope is encapsulated to within the function only and no longer knows about ponent variables.

In order to reference ponent variables, you must utilize lambda expressions instead:

@Component({
  selector: 'app-browse',
  templateUrl: './browse.ponent.html',
  styleUrls: ['./browse.ponent.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'); }

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745064970a4609200.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信