2

我正在使用父子组件。在子组件中,我使用搜索框。我需要随时按需获取搜索框值。如何得到这个?

子组件 html - GridViewComponent

<input id="successFilter" name='filterInput' type="text" (keyup)="searchSuccess($event)">

父组件 ts

@ViewChild(GridviewComponent) grdView: GridviewComponent;

在构造函数之前写上一行

this.grdView.filterInput.subscribe(data => {
  console.log(data);
});

无法按需获取输入值

4

1 回答 1

2

网格视图 html

<input id="successFilter" name='filterInput' [(ngModel)]="filterInput" type="text" (keyup)="searchSuccess($event)">

GridView 打字稿

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);
  }
}

ParentComponent 打字稿,在需要获取值时调用 getChildValue()

@ViewChild('gridView') grdView;

getChildValue() {
   this.grdView.getValue();
}

receivedValue(theValue: string) {
   //this is where you actually get theValue
}

父组件 html

<app-gridView #gridView (sendValue)="receivedValue($event)"></app-gridView>
于 2018-03-06T19:03:08.167 回答