如何在函数中绑定它?

时间:2017-03-16 10:08:47

标签: angular

我有这个功能但是当我把这行开始时:        submitLogin(values){ // Variable to hold a reference of addComment/updateComment let loginOperation:Observable<any>; loginOperation = this.loginService.Login(values); loginOperation.subscribe( function(response) { console.log("Success Response" + response)}, function(error) { console.log("Error happened" + error)}, function(){ this.router.navigate(['/home']); console.log("the subscription is completed"); } ); } 它有效,但当我把它放在我现在的代码中时,它不起作用。有什么建议吗?

enzyme= 'TC'
String = 'AAATTTCCCGGGTCGGGAAA'

import re

#with re.split:
print(list( filter(bool, re.split(r'(.*?{})(?={})'.format(enzyme[0], enzyme[1]), String)) ))

#alternative with re.findall:
print( re.findall(r'.*?{}(?={})|.+$'.format(enzyme[0], enzyme[1]), String) )

4 个答案:

答案 0 :(得分:2)

使用ES6语法忽略此问题或将this的实例存储到变量

submitLogin(values){
 var vm = this;
      // Variable to hold a reference of addComment/updateComment
         let loginOperation:Observable<any>;
         loginOperation = this.loginService.Login(values);
         loginOperation.subscribe(
            function(response) { console.log("Success Response" + response)},
            function(error) { console.log("Error happened" + error)},
            function(){
              vm.router.navigate(['/home']);
               console.log("the subscription is completed");
             }
        );

   }

或者您可以使用ES6

执行此操作
submitLogin(values){
      // Variable to hold a reference of addComment/updateComment
         let loginOperation:Observable<any>;
         loginOperation = this.loginService.Login(values);
         loginOperation.subscribe(
           (response) => { console.log("Success Response" + response)},
           (error) => { console.log("Error happened" + error)},
           () => {
              this.router.navigate(['/home']);
               console.log("the subscription is completed");
             }
        );

   }

答案 1 :(得分:1)

您需要使用当前,因为javascript中无法识别this

submitLogin(values){
    var current = this;
    Variable to hold a reference of addComment/updateComment
    let loginOperation:Observable<any>;
    loginOperation = this.loginService.Login(values);
    loginOperation.subscribe(
    function(response) { console.log("Success Response" + response)},
    function(error) { console.log("Error happened" + error)},
    function(){ current.router.navigate(['/home']);
                console.log("the subscription is completed"); });
}

答案 2 :(得分:1)

当您使用角度2时,请使用ARROW FUNCTIONS () => {}

loginOperation.subscribe((response: any) => {
          console.log("Success Response" + response)
  }, (error: any) => {
          console.log("Error happened" + error)
  }, () => {
          this.router.navigate(['/home']);
          console.log("the subscription is completed");
});

答案 3 :(得分:0)

在订阅处理程序中使用function关键字会导致创建新的JavaScript范围。实际上,这意味着在这些处理程序中,this关键字指向其他内容,而不是您的类。

围绕它的两种方法是在订阅处理程序之外捕获对组件的引用,并在需要访问组件成员时使用该引用,或者通过使用es2015样式箭头函数声明来完全绕过问题引入新范围,因此不会影响此关键字的值

方法1:

onLogin() {
  let component = this;
   ...
  loginOperation.subscribe(
        function(response) { console.log("Success Response" + response)},
        function(error) { console.log("Error happened" + error)},
        function(){
          component.router.navigate(['/home']);
           console.log("the subscription is completed");


         }
    );


}

方法2(优选):

onLogin() {
     let loginOperation:Observable<any>;
     loginOperation = this.loginService.Login(values);
     loginOperation.subscribe(
        (response) => { console.log("Success Response" + response)},
        (error) => { console.log("Error happened" + error)},
        () => {
          this.router.navigate(['/home']);
           console.log("the subscription is completed");

         }
    );