angularjsangular-filters

Angular Filter in AngularJs


var app = angular.module('pixmall', []);
app.controller('myDashboard', function($scope, $http) {
    app.filter('myRandom', function() {
      return function (input) {
      return Math.round(input);
    }
});
<div ng-app="pixmall" ng-controller="myDashboard">
   <span>{{30.35 | myRandom}}</span>
</div>

I want to use the filter to round the number to the nearest whole number but it is not working, I don't know what is wrong


Solution

  • Separate out the controller and filter, you should not place the filter within the controller code.

    DEMO

    var app = angular.module('pixmall', [])
    app.controller("myDashboard",function($scope){
    
    });
    app.filter('myRandom', function() {
      return function (input) {
          return Math.round(input);
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.7/angular.min.js"></script>
    <div ng-app="pixmall" ng-controller="myDashboard">
       <span>{{30.35 | myRandom}}</span>
    </div>