I have a scope variable '$scope.searchType' which is equal to 'createdAt' by default. In my HTML I have options that change the scope variable value:
<ul class="dropdown-menu" role="menu">
<li><a ng-click="searchType = 'Anything'">Anything</a></li>
<li><a ng-click="searchType = 'Payment'">Payment</a></li>
<li><a ng-click="searchType = 'Total'">Total</a></li>
<li><a ng-click="searchType = 'Tax'">Tax</a></li>
<li><a ng-click="searchType = 'Payment Method'">Payment Method</a></li>
</ul>
In my controller I have the following to listen for changes:
$scope.$watch('searchType', function(newValue) {
if(newValue == 'Payment') {
$scope.searchTypeFilter = 'id';
$scope.searchFiltered = true;
} else if(newValue == 'Total') {
$scope.searchTypeFilter = 'total';
$scope.searchFiltered = true;
} else if(newValue == 'Tax') {
$scope.searchTypeFilter = 'tax';
$scope.searchFiltered = true;
} else if(newValue == 'Payment Method') {
$scope.searchTypeFilter = 'paymentType';
$scope.searchFiltered = true;
} else {
$scope.searchTypeFilter = '';
$scope.searchFiltered = false;
$scope.search = '';
}
}, true);
For some reason the $scope.$watch never gets called. I cannot figure out how this doesn't work since it used to. This is the '$scope.searchType' declaration:
$scope.sortType = 'createdAt';
You don't need to add a $watch because you have a click event that will set your filter variable.
Also your if/else can be simplified with a js object.
Please have a look at the demo below or this fiddle.