AngularJS Custom filter to format values


Example

Another use case for filters is to format a single value. In this example, we pass in a value and we are returned an appropriate true boolean value.

function convertToBooleanValue() {
    return function(input) {
        if (typeof input !== 'undefined' &&
                input !== null &&
                (input === true || input === 1 || input === '1' || input
                        .toString().toLowerCase() === 'true')) {
            return true;
        }
        return false;
    };
}

Which in the HTML would be used like this:

{{isAvailable | convertToBooleanValue}}

Or in a controller like:

var available = convertToBooleanValueFilter(isAvailable);