I have an array which I use to populate a table using ng-repeat. I am applying a filter in the controller, which filters the array based on the values of a certain variable columnFilter
. Now, my table view isn't getting updating if I change that variable. I.e the filter isn't reapplying once the variable changes.
JS
var myApp = angular.module('myApp',[]);
function MyCtrl($scope, $filter) {
$scope.name = 'Superhero';
$scope.col={name:""};
$scope.TableData = [{
name: "2017/03/01-14",
specification: "1wk",
},
{
name: "2017/03/01-17",
specification: "Set-04",
},
{
name: "2017/03/04-11",
specification: "1wk",
},
{
name: "2017/04/01-14",
specification: "1wk",
},
{
name: "2017/03/10-10",
specification: "Set-04",
},
{
name: "2017/03/10-10",
specification: "Set-04",
}
$scope.TableDataFiltered = $filter('filter')($scope.TableData, $scope.col);
}
HTML
<div ng-controller="MyCtrl">
<input type="text" ng-model="col.name">
<table>
<thead>
<tr>
<th>name</th>
<th>spec</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in TableDataFiltered">
<td>{{item.name}}</td>
<td>{{item.specification}}</td>
</tr>
</tbody>
</table>
</div>
To all the answers telling how to use | filter: expression
syntax, I know about that, but the actual app has other filters and such, which require the need to filter in the controller itself. (I provided a minimal example which doesn't make that point very clear, that's on me. )