简体   繁体   English

Angular 8,如何在 javascript 库的函数中引用组件属性

[英]Angular 8, how to reference a component property inside a function of a javascript library

I am using Semantic UI in angular and I have the following code:我在 angular 中使用语义 UI,我有以下代码:

componentProperty: boolean = false;

 ngOnInit() {    
    (<any>$('.ui.dropdown')).dropdown();
    
    (<any>$('.ui.input')).popup({
        on: 'focus', 
        onShow: (e) => {
            return this.componentProperty;
        }
    });
}

the function popup is defined by semantic UI.功能弹出是由语义 UI 定义的。 Semantic UI relies on Jquery.语义 UI 依赖于 Jquery。 Here componentProperty is undefined.这里的 componentProperty 是未定义的。

This about scope.这是关于范围的。 Try:尝试:

 ngOnInit() {
    const me = this;

    (<any>$('.ui.dropdown')).dropdown();
    
    (<any>$('.ui.input')).popup({
        on: 'focus', 
        onShow: (e) => {
            return me.componentProperty;
        }
    });
}

Have you tryed to define the onShow function in the components scope?您是否尝试过在组件范围内定义 onShow 函数?

ngOnInit() {    
   const onShow = () => this.componentProperty;
   (<any>$('.ui.input')).popup({
      on: 'focus', 
      onShow
   });
}

And if that still won't work:如果这仍然不起作用:

  const doOnShow = () => this.componentProperty;
  (<any>$('.ui.input')).popup({
      on: 'focus', 
      onShow: doOnShow.bind(this)
   });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 库函数,用于对javascript对象进行安全属性引用 - library function for safe property reference on javascript objects 如何在Angular 4组件中添加Javascript库? - How to add a Javascript library to Angular 4 component? 如何在 Angular 组件中使用 Javascript 库 - How to use Javascript Library in Angular Component 如何在发出的 Highcharts 选择事件回调函数中获取角度组件类引用? - How to get the angular component class reference inside an emitted Highcharts selection event callback function? Angular 组件使用 Javascript 库 function 抛出类型错误:不是 Z86408593C34AF77FDD1Z0DF93 - Angular Component using Javascript Library function throwing Type Error: is Not a Function 如何在JavaScript函数中引用不同的位置 - How to reference different locations inside a JavaScript function How can I use my logo slider JavaScript function inside an Angular component / TypeScript? - How can I use my logo slider JavaScript function inside an Angular component / TypeScript? 如何将javascript文件导入angular4项目并在组件内部使用它的功能? - How to import javascript file into angular4 project and use it's function inside component? 使用angular5组件中的Javascript函数处理HTML元素 - Manipulate HTML element using Javascript function inside angular5 component 如何在反应组件内引用外部 Javascript 文件 - How to reference external Javascript file inside react component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM