Romesh
Romesh

Reputation: 2274

Ng-Repeat Filter Empty string

I have tried to filter blank string item from ng-repeat. But didn't succeeded. Can someone please help me at this.

JSFiddel Link

HTML Code:

<div ng-app ng-controller="myCtrl">
    <ul>
        <li ng-repeat="detail in details | filter: filter2 ">
            <p>{{detail.name}}</p>
        </li>
    </ul>
</div>

JS Code:

function myCtrl($scope) {
    $scope.details = [{
        name: 'Bill',
        shortDescription: null
    }, {
        name: 'Sally',
        shortDescription: 'A girl'
    },{
        name: 'Tally',
        shortDescription: ''
    }];
}

function filter2(detail){
    if (item.shortDescription.length == 0){
        return true;
    }
    else{
        return false;
    }
}

Expected Result:

/*
Expected Result

Bill
Sally
*/

Upvotes: 1

Views: 4513

Answers (1)

Daniel
Daniel

Reputation: 6491

<div ng-app ng-controller="myCtrl">
    <ul>
        <li ng-repeat="detail in details" ng-if="detail.shortDescription">
            <p>{{detail.name}}</p>
        </li>
    </ul>
</div>

fiddle

Upvotes: 2

Related Questions