根据输入模型筛选列表,并在Angular中选择

时间:2014-04-13 08:16:28

标签: javascript angularjs

根据这个jsbin我有一个输入,一个选择选项和一个电影列表。我想当用户在输入中键入内容然后在选择中选择一个选项时,Angular会过滤电影列表。

<div ng-controller="myController">
<input type="text" ng-model="search"/>
<select>
  <option ng-repeat="item in dropdown" value="{{item}}">{{item}}</option>
</select>

<br/>
<ul>
  <li ng-repeat="movie in movies">
    Name: <strong>{{movie.name}}</strong> |
    director: <strong>{{movie.director}}</strong> |
     actor: <strong>{{movie.actor}}</strong>

  </li>
</ul>

我的控制器是这样的:

var myApp = angular.module('myApp',[]);
myApp.controller('myController',function myController($scope){

$scope.dropdown = ['name','director','actor'];

$scope.movies = [

{name:'test',director:'test di',actor:'test ac'},
 {name:'test2',director:'test2 di',actor:'test2 ac'},
 {name:'test3',director:'test3 di',actor:'test3 ac'},
 {name:'test4',director:'test4 di',actor:'test4 ac'}
];
});

1 个答案:

答案 0 :(得分:2)

您需要在ng-repeat中为movielist设置过滤器对象。文档在这里:http://docs.angularjs.org/api/ng/filter/filter

<li ng-repeat="movie in movies | filter:filterObj">

由于对象是动态的,我只能想到在改变输入/选择时生成它。

$scope.changeFilter = function () {
  $scope.filterObj = {};
  $scope.filterObj[$scope.item] = $scope.search;  
};

除非可以看到和编辑代码,否则你的jsbin是无用的。这是一个有效的fiddle