Last active
August 29, 2015 13:56
-
-
Save congjf/8997007 to your computer and use it in GitHub Desktop.
AngularJS Using Filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AngularJS Using Filter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html ng-app="MyReverseModule"> | |
<head> | |
<script src="http://code.angularjs.org/1.2.12/angular.min.js"></script> | |
<script src="script.js"></script> | |
</head> | |
<body> | |
<div ng-controller="Ctrl"> | |
<input ng-model="greeting" type="greeting"><br> | |
No filter: {{greeting}}<br> | |
Reverse: {{greeting|reverse}}<br> | |
Reverse + uppercase: {{greeting|reverse:true}}<br> | |
</div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('MyReverseModule', []). | |
filter('reverse', function() { | |
return function(input, uppercase) { | |
var out = ""; | |
for (var i = 0; i < input.length; i++) { | |
out = input.charAt(i) + out; | |
} | |
// conditional based on optional argument | |
if (uppercase) { | |
out = out.toUpperCase(); | |
} | |
return out; | |
} | |
}); | |
function Ctrl($scope) { | |
$scope.greeting = 'hello'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment