Here is an example code:
Controller:
$scope.filterText = {status: 'Todos', tipo: 'Todos'};
$scope.params = {page: 1, total: 10, orderBy: 'id', sort: 'desc', search: {status: '',tipo: ''}};
$scope.filterStatus = function(status, text) {
$scope.params.search.status = status;
$scope.filterText.status = text;
getProjetos($scope.params);
};
$scope.filterTipo = function(tipo, text) {
$scope.params.search.tipo = tipo;
$scope.filterText.tipo = text;
getProjetos($scope.params);
};
HTML:
<a class="btn btn-default" ng-click="filterStatus(1, 'Execução')">Status: {{filterText.status}}</a>
<a class="btn btn-default" ng-click="filterTipo(1, 'Laboratório')">Tipo: {{filterText.tipo}}</a>
To change the text of the buttons and the parameters of the query I do the assignment inside the function without going through the parameter, directly changing the $ scope, but it should not be a good practice.
I change the $scope.params
because I want the query state to be saved, for example if I click the two buttons, the first arrow the $scope.params.status = 1
and does the search, when I click on the second arrow the $scope.params.tipo = 1
e does the search, still holding the $scope.params.status = 1
in the second search.
What are the best practices for changing variables $scope
and what is the best solution in this situation?