angular 筛选小例子

<!DOCTYPE html>
<html ng-app="myApp">
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="js/angular.min.js"></script>
    <script src="js/jquery-3.1.0.js"></script>
    <link rel="stylesheet" href="css/bootstrap.css"/>
</head>
<body>
    <div class="row-fluid">
        <div class="span3 " ng-controller="FilterCtrl">
            <form class="form-horizontal">
                <div class="controls-row">
                    <label for="searchTextBox" class="control-label">Search:</label>
                        <div class="controls">
                            <input type="text" id="searchTextBox" ng-model="filterService.searchText"/>
                        </div>
                </div>
                <div class="controls-row">
                    <label for="sportComboBox">Sport:</label>
                    <div class="controls">
                        <select  id="sportComboBox" ng-model="filterService.activeFilters.sport">
                            <option ng-repeat="sport in ['BasketBall','Hockey','Football']">
                                {{sport}}
                            </option>
                        </select>
                    </div>
                </div>
                <div class="controls-row">
                    <label for="cityComboBox" class="controls-label">Citys:</label>
                    <div class="controls">
                        <select id="cityComboBox" ng-model="filterService.activeFilters.city">
                            <option ng-repeat="city in ['Dallas','Los Angeles','Boston','New York']">
                                {{city}}
                            </option>
                        </select>
                    </div>
                </div>
                <div class="controls-row">
                    <label class="control-label">Featured:</label>
                    <div class="controls">
                        <input type="checkbox" ng-model="filterService.activeFilters.featured" ng-false-value=""/>
                    </div>
                </div>
            </form>
        </div>
        <div class="offset1 span8" ng-controller="ListCtrl">
            <table>
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Sport</th>
                        <th>City</th>
                        <th>Featured</th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="team in teamsList | filter:filterService.activeFilters | filter:filterService.searchText">
                        <td>{{team.name}}</td>
                        <td>{{team.sport}}</td>
                        <td>{{team.city}}</td>
                        <td>{{team.featured}}</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</body>
<script>
    angular.module("myApp.services",[]).
            factory("filterService",function(){
                return {
                    activeFilters:{},
                    searchText:"",
                }
            })
    var app=angular.module("myApp",["myApp.services"]);
    app.controller("ListCtrl",function($scope,filterService){
        $scope.filterService=filterService;
        $scope.teamsList=[
            {
                id:1,name:"1asdasd",sport:"BasketBall",
                city:"Dallas",featured:true,
            },
            {
                id:2,name:"2asdasd",sport:"BasketBall",
                city:"Dallas",featured:true,
            },
            {
                id:3,name:"3asdasd",sport:"Football",
                city:"New York",featured:true,
            },
            {
                id:4,name:"4asdasd",sport:"Hockey",
                city:"Los Angeles",featured:true,
            },
            {
                id:5,name:"5asdasd",sport:"BasketBall",
                city:"Dallas",featured:false,
            }
        ];
    });
    app.controller("FilterCtrl",function($scope,filterService){
        $scope.filterService=filterService;
        $scope.$watch(filterService,function(old,newVal){
            console.log(newVal)
        })
    })
</script>
</html>

问一下上面下拉框选择之后筛选有问题,但不知道是哪里出问题了?

阅读 2.3k
1 个回答

我给你改好了一个,你对照的看看吧。我感觉如果加上value以后就不会有现在这个问题了。

<!DOCTYPE html>
<html ng-app="myApp">
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
    <link href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
 
</head>
<body>
    <div class="row-fluid">
        <div class="span3 " ng-controller="FilterCtrl">
            <div style="background-color:#333;color:#fff">
                <p>filterService.searchText {{filterService.searchText}}</p>
                <p>filterService.activeFilters.sport {{filterService.activeFilters.sport}}</p>
                <p>filterService.activeFilters.city {{filterService.activeFilters.city}}</p>
                <p>filterService.activeFilters.featured {{filterService.activeFilters.featured}}</p>
                <p>filterService.activeFilters {{filterService.activeFilters}}</p>
                
                
            </div>
            <form class="form-horizontal">
                <div class="controls-row">
                    <label for="searchTextBox" class="control-label">Search:</label>
                        <div class="controls">
                            <input type="text" id="searchTextBox" ng-model="filterService.searchText"/>
                        </div>
                </div>
                <div class="controls-row">
                    <label for="sportComboBox">Sport:</label>
                    <div class="controls">
                        <select  id="sportComboBox" ng-model="filterService.activeFilters.sport">
                            <option ng-repeat="sport in ['','BasketBall','Hockey','Football']">{{sport}}</option>
                        </select>
                    </div>
                </div>
                <div class="controls-row">
                    <label for="cityComboBox" class="controls-label">Citys:</label>
                    <div class="controls">
                        <select id="cityComboBox" ng-model="filterService.activeFilters.city">
                            <option ng-repeat="city in ['','Dallas','Los Angeles','Boston','New York']">
                                {{city}}
                            </option>
                        </select>
                    </div>
                </div>
                <div class="controls-row">
                    <label class="control-label">Featured:</label>
                    <div class="controls">
                        <input type="checkbox" ng-model="filterService.activeFilters.featured" ng-false-value=""/>
                    </div>
                </div>
            </form>
        </div>
        <div class="offset1 span8" ng-controller="ListCtrl">
            <table>
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Sport</th>
                        <th>City</th>
                        <th>Featured</th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="team in teamsList | filter:filterService.searchText | filter:filterService.activeFilters ">
                        <td>{{team.name}}</td>
                        <td>{{team.sport}}</td>
                        <td>{{team.city}}</td>
                        <td>{{team.featured}}</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</body>
<script>
    angular.module("myApp.services",[]).
            factory("filterService",function(){
                return {
                    activeFilters:{},
                    searchText:"",
                }
            })
    var app=angular.module("myApp",["myApp.services"]);
    app.controller("ListCtrl",function($scope,filterService){
        $scope.filterService=filterService;
        $scope.teamsList=[
            {
                id:1,name:"1asdasd",sport:"BasketBall",
                city:"Dallas",featured:true,
            },
            {
                id:2,name:"2asdasd",sport:"BasketBall",
                city:"Dallas",featured:true,
            },
            {
                id:3,name:"3asdasd",sport:"Football",
                city:"New York",featured:true,
            },
            {
                id:4,name:"4asdasd",sport:"Hockey",
                city:"Los Angeles",featured:true,
            },
            {
                id:5,name:"5asdasd",sport:"BasketBall",
                city:"Dallas",featured:false,
            }
        ];
    });
    app.controller("FilterCtrl",function($scope,filterService){
        $scope.filterService=filterService;
        $scope.$watch(filterService,function(old,newVal){
            console.log(newVal)
        })
    })
</script>
</html>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题