我正在尝试开发angularjs过滤器,但我无法在下面的代码中得到结果,只显示一个范围过滤器。另一个过滤器不起作用。我怎么修理我? 如何从国家网站输入表格中过滤两个值?
<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>
答案 0 :(得分:0)
您覆盖了您的对象。解决方案是:
而不是这个$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.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 = '';
}
}
};