通过AngularJS中的多个复选框进行过滤

Par*_*tal 6 angularjs angular-filters

我第一次在这里发布了一个问题,如果我违反任何Stack Overflow礼仪,请提前道歉!:-)

我第一次参加一些AngularJS,以便为我的老板创建一个概念验证.这是一个基本的汽车租赁列表应用程序,主列中有结果列表,侧面有一个过滤器面板.我已设法从JSON对象中提取结果,并应用基本过滤器,如下所示;

<article data-ng-repeat="result in results | filter:search" class="result">
    <h3>{{result.carType.name}}, &pound;{{result.price.value}}</h3>
    <img class="car-type" alt="{{result.carType.name}}" src="{{result.carType.image}}" />
    <ul class="result-features">
            <li>{{result.carDetails.hireDuration}} day hire</li>
            <li data-ng-show="result.carDetails.airCon">Air conditioning</li>
            <li data-ng-show="result.carDetails.unlimitedMileage">Unlimited Mileage</li>
            <li data-ng-show="result.carDetails.theftProtection">Theft Protection</li>
    </ul>
</article>
Run Code Online (Sandbox Code Playgroud)

过滤器

<fieldset>
Doors: 
<select data-ng-model="search.carDetails">
    <option value="">All</option>
  <option value="2">2</option>
  <option value="4">4</option>
</select>   

</fieldset>
Run Code Online (Sandbox Code Playgroud)

...但是我还没有能够解决的一件事是,如何添加一组复选框来应用过滤器,比如说,'car type'会有'mini','compact'等选项, 'family'等等 - 用户可以一次过滤一个或多个选项.我知道我需要使用'ng-model',也许'ng-change',我只是不知道如何将它应用于一组复选框......?

更新:我已经创建了一个plunker,所以你可以看到我在哪里:http://plnkr.co/edit/lNJNYagMC2rszbSOF95k?p = preview

mar*_*nve 8

我会将所有复选框绑定到一个对象上:

app.js

$scope.cartypes = {mini: false, compact:false};
Run Code Online (Sandbox Code Playgroud)

的index.html

<input type="checkbox" data-ng-model="cartypes.mini"> Mini
<input type="checkbox" data-ng-model="cartypes.compact"> Compact
Run Code Online (Sandbox Code Playgroud)

然后创建一个自定义过滤器函数,该函数返回对象是否包含已检查选项的全部(我假设你想要的).

app.js

app.filter('myfilter', function() {
    return function(items, options ) {
      // loop over all the options and if true ensure the car has them
      // I cant do this for you beacause I don't know how you would store this info in the car object but it should not be difficult
      return carMatches;
    };
});
Run Code Online (Sandbox Code Playgroud)

然后你可以将它添加到你的模板,如下所示:

的index.html

<article data-ng-repeat="result in results | filter:search | myfilter:cartypes" class="result">
Run Code Online (Sandbox Code Playgroud)