简体   繁体   中英

ng-hide not working with two filters angularjs

I'm having trouble using two filters with ng-hide. Here's a fiddle that displays my problem: http://jsfiddle.net/czPf6/2/ Here's my code that conditionally selects which filter to apply based on input values in an input box and a select box:

function applySearchFilter() {
        var filterCategory = $scope.filters.cat.toLowerCase();
        //console.log(filterCategory);
        var category = $scope.friend.job.toLowerCase();
        //console.log(category);
        var filter = $scope.filters.name.toLowerCase();
        //console.log(filter);
        var name = $scope.friend.name.toLowerCase();
        console.log(name + " " + category);
        console.log("filter:" + filter + "/category:" + filterCategory);
        if($scope.filters.name == "" && $scope.filters.cat !== ""){
            var isCategory = (filterCategory == category);
            console.log(isCategory + "1st");
            $scope.isExcludedByFilter =  ! isCategory;
            console.log($scope.isExcludedByFilter);
        } else if($scope.filters.name !=="" && $scope.filters.cat == ""){

            var isSubstring = ( name.indexOf( filter ) !== -1 );
            console.log(isSubstring + "2nd");
            $scope.isExcludedByFilter = ! isSubstring;
            console.log($scope.isExcludedByFilter);
        } else if($scope.filters.name !=="" && $scope.filters.cat !==""){

            var isCategory = (filterCategory == category);
            var isSubstring = ( name.indexOf( filter ) !== -1 );
            console.log(isCategory + "3rd");
            console.log(isSubstring + "3rd");
            $scope.isExcludedByFilter = ! isSubstring && ! isCategory;
            console.log($scope.isExcludedByFilter);
        } 

The log statements print out what I would expect them to be, but the filter only works when both input fields have a value. What am I doing wrong here?

It's your isExcludedByFilter thing at the end that's causing it:

$scope.isExcludedByFilter = ! isSubstring || ! isCategory;

I've commented that line out.

I've changed:

} else if($scope.filters.name !=="" && $scope.filters.cat == ""){

    var isSubstring = ( name.indexOf( filter ) !== -1 );
    $scope.isExcludedByFilter = ! isSubstring && ! isCategory; // added && !isCategory

} else if($scope.filters.name !=="" && $scope.filters.cat !==""){

    var isCategory = (filterCategory == category);
    var isSubstring = ( name.indexOf( filter ) !== -1 );

    $scope.isExcludedByFilter = ! isSubstring || ! isCategory; // changed && to ||
}

i also added the following at the end of the above if statement for the "Any" filter. Which means it'll show all 'friends' in the list:

        } else if($scope.filters.name =="" && $scope.filters.cat == ""){
             $scope.isExcludedByFilter = false;
        }

fiddle: http://jsfiddle.net/czPf6/10/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM