如何将函数变量参数传递给angular 2 http subscribe回调?

时间:2017-02-08 12:40:16

标签: angular typescript

我试过了

@RunWith(SpringRunner.class)
@SpringBootTest
public class ExampleTest {

@Autowired
private Example example;

   @Test
   public void getTwoTest() {

       assertEquals(new Integer(2), example.getTwo());
   }

}

@TestComponent
public class Example {

   public Integer getTwo(){
       return 2;
   }

}

我遇到的问题是this.propositionService.addProposition(this.proposition) .subscribe(this.addSuccessCallback, this.addFailureCallback); addSuccessCallback无法访问对象。我不想使用嵌入式lambda表达式。它使代码更不干净。

1 个答案:

答案 0 :(得分:1)

您可以bind组件的this上下文回调,如下所示:

this.propositionService.addProposition(this.proposition)
        .subscribe(this.addSuccessCallback.bind(this), this.addFailureCallback.bind(this));