简体   繁体   English

如何在角度2中从模块到我的组件访问变量的值?

[英]How to access the value of a variable from a module to my component in angular 2?

So this is my app.module.ts (i've injected NgxTypeaheadModule like this) 所以这是我的app.module.ts (我已经注入了NgxTypeaheadModule

   import { NgxTypeaheadModule } from '../components/common/components/ngx-typeahead/ngx-typeahead'    
   imports: [NgxTypeaheadModule]

This is the module ( ngxTypeahead.component.ts ) 这是模块( ngxTypeahead.component.ts

This is ngxTypeahead.component.ts file inside the above module 这是上述模块内的ngxTypeahead.component.ts文件

export class NgxTypeAheadComponent implements OnInit, OnDestroy {
      showSuggestions = false;

      somefunc(){
       showSuggestions = true;
   }

}

In the above module component file, showSuggestions variable will change according to user click. 在上述模块组件文件中, showSuggestions variable将根据用户单击而更改。


I'm using the above module in my component file below( chat.component.ts ) 我在下面的组件文件中使用上述模块( chat.component.ts

 export class ChatComponent implements OnInit, OnDestroy {
       @ViewChild("input") input: ElementRef;

       onKey(event) {
            if (userInput.length == 0)) {

                this.input.showSuggestions = false;
            }
        }
    }

This is my chatComponent.html file 这是我的chatComponent.html文件

 <input #input [(ngModel)]="inputText" ngxTypeahead (keyup)="onKey($event)">

But if i use this.input.showSuggestions , i'm getting the value ,but i'm getting this error 但是,如果我使用this.input.showSuggestions ,我得到的价值,但我得到这个错误

在此处输入图片说明

So Which is the correct way to pass the showSuggestions value from module to my chat.component.ts ?? 那么将showSuggestions值从模块传递到我的chat.component.ts的正确方法是?

Any suggestions or solution would be appreciated!! 任何建议或解决方案将不胜感激!

You could @ViewChild("input") input: NgxTypeAheadComponent; 您可以@ViewChild("input") input: NgxTypeAheadComponent; and get the component instance instead of the element ref, but that's bad practice and a path to violating unidirectional data flow! 并获取组件实例而不是元素ref,但这是一种不好的做法,并且是违反单向数据流的路径! Can also break change detection, doesn't work with OnPush. 还可以中断更改检测,不适用于OnPush。

Use @Input instead: @Input() showSuggestions = false; 改用@Input: @Input() showSuggestions = false; and in ChatComponent: 并在ChatComponent中:

<input #input [(ngModel)]="inputText" ngxTypeahead 
(keyup)="onKey($event)" [showSuggestions]="showSuggestions">

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

相关问题 Angular:如何在Module中声明变量并从所有组件访问它? - Angular : How declare variable at Module and access it from all component? 如何以角度2访问不同模块的组件 - How to access a component from different module in angular 2 如何以角方式将变量从一个组件访问另一个组件? - How to access variable from one component into another component in angular? 如何在我的 app.module 中访问 const(不导出 const 变量)变量并从 angular 中的子组件访问它? - How to access const(without export const variable) variable in my app.module and access it from children components in angular? 如何使用Angular 7从另一个组件访问组件值 - How to access component value from another component with Angular 7 如何将Angular Material自动完成中的值分配给组件中的变量? - How can I assign a value from the Angular Material autocomplete to a variable in my component? 从另一个模块/窗口访问Angular 2组件 - Access Angular 2 component from another module / window 无法从 angular 8 中的不同模块访问组件? - Unable to access component from different module in angular 8? 如何从 Angular 9 中的组件访问服务类中定义的变量? - How to access a variable defined in service class from a component in Angular 9? 如何从 angular 组件中的自定义 javascript 访问变量? - How to access variable from custom javascript in angular component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM