如何在jQuery函数中更改this.variable?

时间:2018-07-03 11:11:02

标签: jquery typescript angular5

如何在jquery函数中更改AuthDirective类的this.variable?

也许是愚蠢的问题,但是,我需要使用Jquery事件并更改/使用角度变量。

import { Component, Directive, OnInit } from '@angular/core';
import { AppGlobal                    } from '../../../app.global';
declare var $:any;

@Component({
  selector    : 'authorization',
  templateUrl : './auth.directive.html'
})
export class AuthDirective {
  isCaptcha : boolean = false;
  constructor(
    public  app : AppGlobal
  ) {
  }
  ngOnInit() {
    $('.sign-up-label').on('show.bs.modal', function (e) {
      this.isCaptcha = true; // NOT IN THIS CONTEXT, IN THAT CONTEXT
    })
  }
}

{{isCaptcha}}始终为假

<div class="form-group ta-web-right mt-3" *ngIf="isCaptcha">
  <re-captcha (resolved)="resolved($event)"></re-captcha>
</div>

请帮助。

2 个答案:

答案 0 :(得分:0)

您可以通过将外部变量DataTemplate的值分配给变量来保留它:

this

}

答案 1 :(得分:0)

像这样let self = this;存储外部上下文,然后它与本地上下文不冲突。

像这样替换您的function

ngOnChanges(changes: SimpleChange) {
    let self = this;
    $('.sign-up-label').on('show.bs.modal', function (e) {
      self.isCaptcha = true; // NOT IN THIS CONTEXT, IN THAT CONTEXT
    })
  }