jquery - 如何在 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>
请帮忙。
解决方案
this
您可以通过将其分配给变量来保留外部上下文的值:
ngOnInit() {
var self = this;
$('.sign-up-label').on('show.bs.modal', function (e) {
self.isCaptcha = true;
})
}
推荐阅读
- python - 如何在python中编写一个像jasmine这样的嵌套测试用例
- wordpress - Postfix 不会向 Gmail、Outlook 发送电子邮件。多个域和 Wordpress
- reactjs - 为什么更新我的列表不会触发我的组件的重新渲染?
- django - 如何在 djangoreostframework-simplejwt 中使用电子邮件而不是用户名?
- kotlin - Kotlin 从 txt.file 读取/导入 Map
- teamcity - TeamCity 在通用分支名称上成功构建后触发构建
- python - 将新的匀称点匹配到一系列匀称的多边形 - 快速
- azure - 在 FTP 上移动文件
- javascript - JavaScript算术减法给出额外不需要的十进制数字
- raml - RAML 合并说明