angularJS中的有没有截取字符串的过滤器?比如我要把后台的数据调用显示在HTML中,{{浙江省杭州市萧山区 | ????}},只显示“萧山”。如果没有自带过滤器,自己可以定义过滤器吗?
<div ng-app="filterExample">
<div ng-controller="MainCtrl">
<h3>{{ originalText}}</h3>
<h3>{{ filteredText}}</h3>
</div>
</div>
angular.module('filterExample', [])
.filter('intercept', function() {
return function(input) {
if (input) {
return input.slice(0, input.length - 1);
}
}
})
.controller('MainCtrl', function($scope, $filter, $filter) {
$scope.originalText = '萧山区';
$scope.filteredText = $filter('intercept')($scope.originalText);
});
如何使用filter