basil basil - 3 months ago 22
AngularJS Question

angularjs filters function not working

I am trying to develop angularjs filter but I can't get result in the code below to show only one scope filter. Another filter is not working. How can I fix i?
How to filter two value from input form like country site?

<html >
<head>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
<title>search</title>
</head>
<body>
<div ng-app="app" ng-controller="mainCtrl">
<div>Search by Name: <input type="text" ng-model="filters.search"></div>
<div>Show only Company X: <input type="checkbox" ng-model="filters.x" ng-change="actions.updateCompany()"/>
</div>
<div>Show only company Y:<input type="checkbox" ng-model="filters.y" ng-change="actions.updateyCompany()"/>
<div ng-repeat="arr in array | filter:filters.search | filter:{company: filters.company}">
<span ng-bind="arr.name"></span>
</div>
</div>

<script type="text/javascript" src="js/angular.min.js"></script>
<script src="js/ui-bootstrap-tpls-0.9.0.js"></script>
<script type="text/javascript">
var app = angular.module('app', []);
app.controller('mainCtrl', function($scope) {
$scope.filters = {
x: false,
company: '',
search: ''
};

$scope.actions = {
updateCompany: function () {
if($scope.filters.x) {
$scope.filters.company = 'x';
} else {
$scope.filters.company = '';
}
}
};

$scope.actions = {
updateyCompany: function () {
if($scope.filters.y) {
$scope.filters.company = 'y';
} else {
$scope.filters.company = '';
}
}
};



$scope.array = [
{name: 'Tobias', lname: 'TLname', company: 'x'},
{name: 'Jeff', lname: 'JLname', company: 'x'},
{name: 'Brian', lname: 'BLname', company: 'x'},
{name: 'Igor', lname: 'ILname', company: 'y'},
{name: 'James', lname: 'JLname', company: 'z'},
{name: 'Brad', lname: 'BLname', company: 'y'}
];
});
</script>


</body>
</html>

Answer

You overwrite your object. The solution is:

instead this

$scope.actions = {
        updateCompany: function () {
            if($scope.filters.x) {
                $scope.filters.company = 'x';
            } else {
                 $scope.filters.company = '';   
            }
        }
    };

     $scope.actions = {
        updateyCompany: function () {
            if($scope.filters.y) {
                $scope.filters.company = 'y';
            } else {
                 $scope.filters.company = '';   
            }
        }
    };

Write this:

$scope.actions = {
        updateCompany: function () {
            if($scope.filters.x) {
                $scope.filters.company = 'x';
            } else {
                 $scope.filters.company = '';   
            }
        },
        updateyCompany: function () {
            if($scope.filters.y) {
                $scope.filters.company = 'y';
            } else {
                 $scope.filters.company = '';   
            }
        }
    };

Working fiddle http://jsfiddle.net/Lvc0u55v/8865/