首页 > 解决方案 > 如何在 jquery 函数中更改 this.variable?

问题描述

如何在 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>

请帮忙。

标签: jquerytypescriptangular5

解决方案


this您可以通过将其分配给变量来保留外部上下文的值:

ngOnInit() {
    var self = this;
    $('.sign-up-label').on('show.bs.modal', function (e) {
      self.isCaptcha = true;
    })

}


推荐阅读