Reputation: 41939
For myLongString
, I'd like to filter it by
(1) keeping the first y
characters and (2) make it all lower-case
HTML
<body ng-controller="MyCtrl">
<span>{{myLongString | limitTo:5 | lowercase}}</span>
</body>
JavaScript
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.myLongString = "EEEEEEEEEEEE";
$scope.limitTo = function(x, y) {
console.log("x:", x, "y:", y);
x.slice(0, y);
}
$scope.lowercase = function () {
$scope.myLongString = $scope.myLongString.toLowerCase();
}
}
The text is all lower-cased, but I don't see the first 5 characters slice. Plus, the console.log
doesn't show up.
Why's that?
Upvotes: 0
Views: 63
Reputation: 74176
Just create a filter
, like:
angular.module('myApp').
filter('substring', function(){
return function(str, length){
return str.substring(0, length);
};
});
Upvotes: 1
Reputation: 18354
I don't know Angular, but made it work:
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.myLongString = "EEEEEEEEEEEE";
//No more needed here
}
myApp.filter('limitTo', [ function() {
return function(str, size) {
return str.slice(0, size);
};
}]);
Note that your lowercase
transform wasn't working, the one that was working was the built in one.
Fiddle: http://jsfiddle.net/edgarinvillegas/r9MTc/11/
Cheers
Upvotes: 0