Javascript @viewchild无法从子组件获取输入元素值
我正在使用父组件和子组件。在子组件中,我使用搜索框。我需要随时根据需要获取搜索框的值。如何得到这个 子组件html-GridViewComponentJavascript @viewchild无法从子组件获取输入元素值,javascript,angular,angular-components,viewchild,Javascript,Angular,Angular Components,Viewchild,我正在使用父组件和子组件。在子组件中,我使用搜索框。我需要随时根据需要获取搜索框的值。如何得到这个 子组件html-GridViewComponent <input id="successFilter" name='filterInput' type="text" (keyup)="searchSuccess($event)"> 在构造函数之前写在上面一行 this.grdView.filterInput.subscribe(data => { console.log(d
<input id="successFilter" name='filterInput' type="text" (keyup)="searchSuccess($event)">
在构造函数之前写在上面一行
this.grdView.filterInput.subscribe(data => {
console.log(data);
});
我无法按需获取输入值GridView html
<input id="successFilter" name='filterInput' [(ngModel)]="filterInput" type="text" (keyup)="searchSuccess($event)">
父组件html
<app-gridView #gridView (sendValue)="receivedValue($event)"></app-gridView>
这是在constructor@ViewChild(GridviewComponent)grdView:GridviewComponent之前;非常感谢。这项工作是否会在任何时候不仅文本在正常时间发生变化?我检查并回复给你如果你想在文本更改的任何时候获得它,你可以将(keyup)函数设置为(keyup)=“getValue()”Kevin,我可以使用该方法获取值,谢谢你的帮助
import { Output, OnInit } from '@angular/core';
@Component({
selector: 'app-gridView',
templateUrl: './gridView.component.html',
styleUrls: ['./gridView.component.scss']
})
export class GridViewComponent implements OnInit {
@Output() sendValue = new EventEmitter<string>();
filterInput = '';
constructor(){}
ngOnInit() {}
getValue() {
this.sendValue.emit(this.filterInput);
}
}
@ViewChild('gridView') grdView;
getChildValue() {
this.grdView.getValue();
}
receivedValue(theValue: string) {
//this is where you actually get theValue
}
<app-gridView #gridView (sendValue)="receivedValue($event)"></app-gridView>