As working on AngularJS we are very accustomed with using filters. But generally the filters we used are in built filter like orderby upper lower and many more. Here I have tried to build two custom filters one of which will reverse the string and another will toggle the letter case. So not extending any more lets jump into the code below and see what it's doing. I have not used a separate script and put everything within an HTML page.
<html ng-app="myApp">
<head>
<title>
</title>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script>
myApp = angular.module('myApp',[]); //Angular module
myApp.controller('MyCtrl',function ($scope) { //Controller
$scope.message="Subhroneel";
$scope.message1="SuBhRoNeEl";
$scope.message2 = "Join, all, text";
});
myApp.filter('reverse',function() { //Custom filter reverse
return function(text){
return text.split("").reverse().join("");
}
});
myApp.filter('toggle',function() { // Custom filter toggle
return function(text) {
var s="";
for(i=0;i<text.length;i++)
{
// If a letter is upper case then it is converted to lower case, if it is lower case then converted
// to upper case and every letter is stored in a separate string and returned finally without
// disturbing the original string.
if(text.charAt(i)==text.charAt(i).toLowerCase())
s+=text.charAt(i).toUpperCase()
else if(text.charAt(i)==text.charAt(i).toUpperCase())
s+=text.charAt(i).toLowerCase()
else
s+=text.charAt(i);
}
return s;
}
});
myApp.filter('concat',function(){
return function(text) {
var s=text.split(",");
var ret = "";
for(i=0;i<s.length;i++)
ret+=s[i];
return ret;
}
});
</script>
</head>
<body ng-controller="MyCtrl">
<input type="text" ng-model="message" name="message" />
Reverse : {{message|reverse}} </br>
<input type="text" ng-model="message1" name="message1" />
Toggle : {{message1|toggle}}
<input type="text" ng-model="message2" name="message2" />
Added Strings : {{message2|concat}} </br>
</body>
</html>
#angularjs #customfilter #javascript #mvc
Reverse : {{message|reverse}}
Toggle : {{message1|toggle}}
Added Strings : {{message2|concat}}
<html ng-app="myApp">
<head>
<title>
</title>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script>
myApp = angular.module('myApp',[]); //Angular module
myApp.controller('MyCtrl',function ($scope) { //Controller
$scope.message="Subhroneel";
$scope.message1="SuBhRoNeEl";
$scope.message2 = "Join, all, text";
});
myApp.filter('reverse',function() { //Custom filter reverse
return function(text){
return text.split("").reverse().join("");
}
});
myApp.filter('toggle',function() { // Custom filter toggle
return function(text) {
var s="";
for(i=0;i<text.length;i++)
{
// If a letter is upper case then it is converted to lower case, if it is lower case then converted
// to upper case and every letter is stored in a separate string and returned finally without
// disturbing the original string.
if(text.charAt(i)==text.charAt(i).toLowerCase())
s+=text.charAt(i).toUpperCase()
else if(text.charAt(i)==text.charAt(i).toUpperCase())
s+=text.charAt(i).toLowerCase()
else
s+=text.charAt(i);
}
return s;
}
});
myApp.filter('concat',function(){
return function(text) {
var s=text.split(",");
var ret = "";
for(i=0;i<s.length;i++)
ret+=s[i];
return ret;
}
});
</script>
</head>
<body ng-controller="MyCtrl">
<input type="text" ng-model="message" name="message" />
Reverse : {{message|reverse}} </br>
<input type="text" ng-model="message1" name="message1" />
Toggle : {{message1|toggle}}
<input type="text" ng-model="message2" name="message2" />
Added Strings : {{message2|concat}} </br>
</body>
</html>
#angularjs #customfilter #javascript #mvc
No comments:
Post a Comment