如何在angular 2中访问从模块到我的组件的变量值?
这就是我的如何在angular 2中访问从模块到我的组件的变量值?,angular,angular2-template,Angular,Angular2 Template,这就是我的app.module.ts(我像这样注入了ngxtypeahdemule) 这是模块(ngxtypeaward.component.ts) 这是上述模块中的ngxtypeaward.component.ts文件 在上面的模块组件文件中,showSuggestions变量将根据用户单击而更改 我正在下面的组件文件中使用上述模块(chat.component.ts) 这是我的chatComponent.html文件 <input #input [(ngModel)]="input
app.module.ts
(我像这样注入了ngxtypeahdemule
)
这是模块(ngxtypeaward.component.ts
)
这是上述模块中的ngxtypeaward.component.ts文件
在上面的模块组件文件中,showSuggestions变量
将根据用户单击而更改
我正在下面的组件文件中使用上述模块(
chat.component.ts
)
这是我的chatComponent.html文件
<input #input [(ngModel)]="inputText" ngxTypeahead (keyup)="onKey($event)">
但是如果我使用this.input.showSuggestions
,我得到的是值,但是我得到的是这个错误
那么,将showSuggestions值从模块传递到mychat.component.ts
的正确方法是什么呢
任何建议或解决方案将不胜感激 您可以@ViewChild(“输入”)输入:NgxTypeAheadComponent代码>并获取组件实例,而不是元素引用,但这是一种糟糕的做法,是违反单向数据流的一条路径!也可以中断更改检测,不适用于OnPush
改为使用@Input:@Input()showSuggestions=false代码>和在聊天室组件中:
<input #input [(ngModel)]="inputText" ngxTypeahead
(keyup)="onKey($event)" [showSuggestions]="showSuggestions">
export class ChatComponent implements OnInit, OnDestroy {
@ViewChild("input") input: ElementRef;
onKey(event) {
if (userInput.length == 0)) {
this.input.showSuggestions = false;
}
}
}
<input #input [(ngModel)]="inputText" ngxTypeahead (keyup)="onKey($event)">
<input #input [(ngModel)]="inputText" ngxTypeahead
(keyup)="onKey($event)" [showSuggestions]="showSuggestions">