Skip to content

Filter lesson mentions usage in controller but lacks example of such #24

@wizonesolutions

Description

@wizonesolutions

I did this, though, and it worked fine. I looked up how here: https://docs.angularjs.org/guide/filter#using-filters-in-controllers-services-and-directives

<!-- index.html -->
<div ng-controller="index">
    <p>Value: <input type="text" ng-model="value" /></p>
    <p>{{ value | round:2 | dollars }}</p>
    <p>{{ testValue }}</p>
</div>
// root.js
angular.module("root", ["filters"])
    .controller("index", ['$scope', "roundFilter", "dollarsFilter", function($scope, roundFilter, dollarsFilter) {
        $scope.testValue = '';
        $scope.$watch('value', function (newValue, oldValue) {
            $scope.testValue = dollarsFilter(roundFilter(newValue, 2));
        });
    }]);

// filters.js
angular.module("filters", [])
    .filter("round", function () {
        return function(input, precision) {
            return input ?
                parseFloat(input).toFixed(precision) :
                "";
        };
    })
    .filter("dollars", function () {
        return function(input) {
            return input ? "$" + input : "";
        };
    });

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions