angularjs: change filter options dynamically

前端 未结 3 1046
南旧
南旧 2020-12-03 19:24

What I want is something like this example in the documentation but with a unique input that can play the three roles of filtering by \"any\", \"name\" or \"phone\" properti

3条回答
  •  清歌不尽
    2020-12-03 19:54

    Here's one approach - there are probably others.


    by {{filter}}

    NamePhone
    {{friend.name}} {{friend.phone}}

    Javascript:

    function MainCtrl($scope, $http) {
        $scope.friends = [{name:'John', phone:'555-1276'},
                          {name:'Mary', phone:'800-BIG-MARY'},
                          {name:'Mike', phone:'555-4321'},
                          {name:'Adam', phone:'555-5678'},
                          {name:'Julie', phone:'555-8765'}];
        $scope.filter = "$";
        $scope.multi = "";
        $scope.changeFilterTo = function(pr) {
            $scope.filter = pr; 
        }
        $scope.getFilter = function() {
            switch ($scope.filter) {
                case 'name':
                    return {name: $scope.multi};
                case 'phone':
                    return {phone: $scope.multi};
                default:
                    return {$: $scope.multi}
            }
        }
    };
    

提交回复
热议问题