Angular 2 Observable订阅不起作用

时间:2017-01-16 13:45:56

标签: angular angular2-observables

我正在尝试订阅一个像init这样的组件的observable:

var app=angular.module("MyApp");

有没有理由说第一个lambda表达式有效,但第二个表达式从不这样做?

编辑:

以下是getTicketAsync返回的代码帽:

    this.ticketService.getTicketsAsync(filters).subscribe(
        tickets => this.ticketResponse = tickets,
        () => console.log('hi'))

2 个答案:

答案 0 :(得分:2)

当观察到抛出错误时,第二个是catch

subscription = source.subscribe(
  x => console.log('onNext: %s', x),
  e => console.log('onError: %s', e),
  () => console.log('onCompleted'));

https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/subscribe.md

<强>解决方案: 1.使用onNext hook:

this.ticketService.getTicketsAsync(filters).subscribe(
    tickets => {
      this.ticketResponse = tickets;
      console.log('hi');
    },
    () => console.log('ERROR!'));

2。使用onCompleted hook:

this.ticketService.getTicketsAsync(filters).subscribe(
  tickets => this.ticketResponse = tickets,
  error => console.log('ERROR: ' +error),
  () => console.log('hi')
);

答案 1 :(得分:0)

大箭头表示使您的代码像这样的函数

this.ticketService.getTicketsAsync(filters)
.subscribe(
  function(tickets){
    this.ticketResponse = tickets,
    function(){
      console.log("hi")
    }
  }
)

您正在传递两个参数(这是两个回调函数)。