我如何通过将数组第一个值作为输入检查来应用过滤器?

How can i Apply filter by taking the arrays First value as Input On check?

我正在学习 AngularJs ...

示例:-

我的 Json 有一个包含一些值作为类型的数组 :-

假设一家餐厅是墨西哥餐厅或意大利餐厅等 我的例子

 {
  name:'res 123',
  description:'Italian Rest'
   types:['Mexican','Indian']
   }

<input type="checkbox" data-ng-model="Mexican"/> // Iam Using Textbox  Oncheck Filter Need to Filter all the Objects with types:['Mexican'] 

 Filter Code :- 
 <div class="col-xs-12" data-ng-repeat="obj in objs| filter : objs.types[1]: Mexican" > <!-- Filter applied Like this -->
 Realted looping 
</div>

我如何通过将类型['Mexican']值作为过滤器检查时的输入来应用过滤器?

Angular 中的内置过滤器接受一个散列,该散列指定与数组中的每个元素匹配的属性。

因此,如果您有一系列餐厅:

var restaurants = [
  { name: "foo", types: ["Mexican", "Indian"] },
  { name: "bar", types: ["Mexican"] },
  { name: "baz", types: ["Italian"] }
];

然后,如果您需要按 name 进行过滤,filter 的输入将是 {name: 'b'} - 这将为您提供 "bar""baz"

同样,如果您需要按 types 进行过滤 - 即使它是一个数组 - 类似的方法也行得通:{types: "Mexican"}.

因此,您只需要构建该对象 - 我们将其命名为 filterBy

<input type="checkbox" ng-model="filterBy.types" 
       ng-true-value="'Mexican'"
       ng-false-value="undefined"> Mexican

<div ng-repeat="r in restaurants | filter: filterBy>
  {{r.name}}
</div>

Demo