Я знаю, что могу фильтровать так:
<input type = "search" ng-model = "searchTable">
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat = "row in $ctrl.rows | filter: searchTable">
<td>{{ row.firstName }}</td>
<td>{{ row.lastName }}</td>
</tr>
</tbody>
</table>
Как мне отфильтровать, если <input type = "search" ng-model = "searchTable"> и <tr ng-repeat = "row in $ctrl.rows | filter: searchTable"> находятся в отдельных компонентах?
Вот простой рабочий пример.
Насколько я понимаю, мне нужно добавить $onChanges() и привязку выражения bindings: { onChange: '&' } к searchComponent, но я не совсем понимаю, как это реализовать.
Спасибо



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Здесь - рабочая рабочий пример. Вы сохраняете поисковый запрос в родительском компоненте и передаете его обоим дочерним компонентам.
var app = angular.module('app', []);
app.controller('appController', function() {
this.search = "";
});
app.component('searchComponent', {
template: `
<h4>{{ $ctrl.title }}</h4>
<label>Search table</label>
<input type = "search" ng-model = "$ctrl.search">
`,
controller: function() {
this.title = 'Search Component';
},
bindings: {
search: " = "
}
});
app.component('tableComponent', {
template: `
<h4>{{ $ctrl.title }}</h4>
<!-- <p>This works</p>
<label>Search table</label>
<input type = "search" ng-model = "searchTable"> -->
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat = "row in $ctrl.rows | filter: $ctrl.search">
<td>{{ row.firstName }}</td>
<td>{{ row.lastName }}</td>
</tr>
</tbody>
</table>
`,
controller: function() {
this.title = 'Table Component';
this.rows = [
{firstName: 'Zulu', lastName: 'Apple'},
{firstName: 'Alice', lastName: 'xRay'},
{firstName: 'Fred', lastName: 'Rogers'}
];
},
bindings: {
search: "<"
}
});body {
font-family: 'Arial', sans-serif;
}
table {
border-collapse: collapse;
}
td, th {
border: 1px solid grey;
padding: 5px;
}
label {
display: block;
}
input {
margin-bottom: 10px;
}<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.7.0/angular.min.js"></script>
<div ng-app = "app">
<div ng-controller = "appController">
<h3>Filter Between Components</h3>
<search-component search = "search"></search-component>
<table-component search = "search"></table-component>
</div>
</div>Вы можете попробовать испускать - на в своем компоненте для связи между ними, как показано в приведенном ниже коде. Также проверьте обновленный jsfiddle с рабочим примером вашего сценария.
Контроллер компонентов поиска:
$scope.$watch('searchTable', function() {
$rootScope.$emit('onEmitSearchData', $scope.searchTable);
});
Контроллер компонентов таблицы:
$rootScope.$on('onEmitSearchData', function(event, data) {
$scope.searchTable = data;
});