LearnAngular
LearnAngular copied to clipboard
Filter lesson mentions usage in controller but lacks example of such
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 : "";
};
});