2014-01-06 59 views
0

對於myLongString,我想通過對其進行過濾鏈式功能

(1)保持第一y字符(2)把它的所有小寫

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(); 
    } 
} 

文本全部是小寫,但我沒有看到前5個字符切片。另外,console.log不顯示。

這是爲什麼?

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

回答

1

只需創建一個filter,如:

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

http://jsfiddle.net/CQZAL/

0

我不知道角,但使工作:

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); 
    }; 
}]); 

請注意,您lowercase變換不工作,這是工作中內置的一個之一。

小提琴:http://jsfiddle.net/edgarinvillegas/r9MTc/11/

乾杯