javascript - 复选框过滤器

标签 javascript html angularjs

我想知道如何创建一个值为“无酒精”的复选框过滤器,以便每次选中它时都会遍历数组并仅选择类型为“无酒精”的啤酒?

var app = angular.module("myModule", []);

app.controller("MainController", function($scope) {
  $scope.beer = [
    { name: "beer1",   price: 20, type:"Alcohol"},
    { name: "beer2",   price: 55, type:"no Alcohol" },
    { name: "beer3",   price: 20, type:"Alcohol" },
    { name: "beer4",   price: 37, type:"no Alcohol" },
    { name: "beer5", price: 20, type:"Alcohol" },
    { name: "beer6",  price: 32, type:"Alcohol" }
  ];
});
<!DOCTYPE html>
<html ng-app="myModule">
  <head>
  	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
  	<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
  	<script src="file:///C:/Users/LG/Documents/Angularteste/Teste/app.js"></script>	
  </head>
  <body>
    <div ng-controller="MainController">
      <form class="form-inline">
        <input type="text" ng-model="test.name">
        <input type="text" ng-model="test.type">
        <input type="checkbox" ng-model="exactMatch"/> Exact Match
      </form>
      <ul ng-repeat="friend in beer | filter:test:exactMatch">
        <li>{{friend.name}}</li>
        <li>{{friend.type}}</li>
      </ul>
    </div>
 		<input type="checkbox" ng-model='search.type1' ng-true-value="no alcohol" ng-false-value='' /> Would you like beer with no alchol? 
  </body>
</html>

最佳答案

您的模板有错误: 而不是

<input type="checkbox" ng-model='search.type1' ng-true-value="no alcohol" ng-false-value='' /> Would you like beer with no alchol?

您需要将 search.type1 更改为 search.typeng-true-valueng-false-value 接受表达式而不是字符串,因此您需要将其更改为:

<input type="checkbox" ng-model="search.type" ng-true-value="'no Alcohol'" ng-false-value="''" /> Would you like beer with no alchol? 

最后一步是添加额外的过滤器

<ul ng-repeat="friend in beer | filter:test:exactMatch | filter:search:true">

这是工作片段

var app = angular.module("myModule", []);

app.controller("MainController", function($scope) {
  $scope.beer = [
    { name: "beer1",   price: 20, type:"Alcohol"},
    { name: "beer2",   price: 55, type:"no Alcohol" },
    { name: "beer3",   price: 20, type:"Alcohol" },
    { name: "beer4",   price: 37, type:"no Alcohol" },
    { name: "beer5", price: 20, type:"Alcohol" },
    { name: "beer6",  price: 32, type:"Alcohol" }
  ];
});
<!DOCTYPE html>
<html ng-app="myModule">
  <head>
  	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
  	<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
  	<script src="file:///C:/Users/LG/Documents/Angularteste/Teste/app.js"></script>	
  </head>
  <body>
    <div ng-controller="MainController">
      <form class="form-inline">
        <input type="text" ng-model="test.name">
        <input type="text" ng-model="test.type">
        <input type="checkbox" ng-model="exactMatch"/> Exact Match
      </form>
      <ul ng-repeat="friend in beer | filter:test:exactMatch | filter:search">
        <li>{{friend.name}}</li>
        <li>{{friend.type}}</li>
      </ul>
    </div>
    <input type="checkbox" ng-model="search.type" ng-true-value="'no Alcohol'" ng-false-value="''" /> Would you like beer with no alchol? 
  </body>
</html>

关于javascript - 复选框过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33827216/

相关文章:

javascript - 计算值的出现次数,例如@@?

javascript - Angular Broadcast 多次触发

javascript - 如何使用timezone-js?

javascript - 动态添加下拉菜单并排除所选选项

javascript - 在 AngularJS 中修改 DOM

javascript - 在 ng-repeat 中使用过滤器

javascript - 如何在每个类中找到 href 属性?

javascript - 承载授权,来自服务器的 400 错误

javascript - 如何使用jquery计算分隔列表中元素的索引?

javascript - 将内容添加到 inline-block-div 会以意想不到的方式弄乱布局