Kevin Meredith
Kevin Meredith

Reputation: 41939

Chained Functions in Filter

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?

http://jsfiddle.net/r9MTc/10/

Upvotes: 0

Views: 63

Answers (3)

CD..
CD..

Reputation: 74176

Just create a filter, like:

angular.module('myApp').
 filter('substring', function(){
     return function(str, length){
         return str.substring(0, length);
     };
 });

http://jsfiddle.net/CQZAL/

Upvotes: 1

Edgar Villegas Alvarado
Edgar Villegas Alvarado

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

user2445933
user2445933

Reputation:

limitTo is inbuilt angualrjs filter, just like lowercase.

Upvotes: 3

Related Questions