Custom Filters in AngularJS

by Yubraj Pokharel

HTML

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<ul ng-app="myApp" ng-controller="namesCtrl">
    <li ng-repeat="x in names">
        {{x | myFormat}}
    </li>
</ul>

JavaScript

var app = angular.module('myApp', []);
  
  //custom filters
  app.filter('myFilter', function(){
  	return function(x){
    var result ="";
    var c;
    	for(var i = 0; i<x.lenght; i++){
      	c = x[i];
        if(c === 'a' || c === 'e' || c === 'i' || c === 'o' || c === 'u'){
        	c = "[V]";
        }else{
        	c = "[C]"
        }
        result += c;
      }
      
      return result;
    }
  })
  
  app.controller('namesCtrl', function($scope){
  	$scope.names = ["name1", "name2", "name3"];
  });