AngularJS 1.0.7:限制过滤器无法正常工作

时间:2017-10-16 17:04:22

标签: angularjs

简单end在Angular1.0.7中不起作用的任何原因?它显示了整个描述。控制台中没有错误。

在html页面中:

yourCubes = [[x,x**2,x**3] for x in range(start, end)]

3 个答案:

答案 0 :(得分:2)

我会建议使用最新版本的angularjs,但它仍然适用于1.0.7

<强>样本

var app= angular.module('testApp',[]);
app.controller('testCtrl',function($scope){
  $scope.boat ={};
  $scope.boat.description ="The length of the returned array or string. If the limit number is positive, limit number of items from the beginning of the source array/string are copied. If the number is negative, limit number of items from the end of the source array/string are copied. The limit will be trimmed if it exceeds array.length. If limit is undefined, the input will be returned unchanged.";
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="testApp" ng-controller="testCtrl">
<hr class="no-margin-top" />
<div class="row-fluid custom-row-margin-20">
    <div class="span12 boat-description">{{boat.description | limitTo: 40}}</div>
</div>
</body>

修改

对于角度1.0.7, limitTo 过滤器仅适用于Array。根据您的要求,您需要实现自定义过滤器。

.filter("limitToCustom",function(){
    return function(actual,number){
        console.log(number)
        var arrData = actual.split("");
        return  actualData.slice(-number);
    }
});

并在 HTML

<h1>Hello {{boat.description |limitToCustom : 40 }}!</h1>

答案 1 :(得分:1)

原始limitTo过滤器仅支持数组。 Angular 1.1.12 changelog中添加了对字符串的支持。

在Angular 1.1.12之前,有必要手动剪切字符串。执行此操作的自定义指令的示例包含在错误跟踪器中;我会把它包括在内(注意,不是我写的,也没有经过全面测试)。

/**
 * Usage:
 *   {{some_text | cut:true:100:' ...'}}
 * Options:
 *   - wordwise (boolean) - if true, cut only by words bounds,
 *   - max (integer) - max length of the text, cut to this number of chars,
 *   - tail (string, default: '&nbsp;&hellip;') - add this string to the input
 *     string if the string was cut.
 */
.filter('cut', function () {
    return function (value, wordwise, max, tail) {
        if (!value) return '';

        max = parseInt(max, 10);
        if (!max) return value;
        if (value.length <= max) return value;

        value = value.substr(0, max);
        if (wordwise) {
            var lastspace = value.lastIndexOf(' ');
            if (lastspace != -1) {
                value = value.substr(0, lastspace);
            }
        }

        return value + (tail || ' …');
    };
});

答案 2 :(得分:-1)

请使用角度js 1.2.10。

<强>样本

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
<div ng-app="testApp" ng-controller="testCtrl">
<div class="row-fluid custom-row-margin-20">
    <div class="span12 boat-description">{{boat.description | limitTo: 50}}</div>
</div>  
</div>

<script>
var app= angular.module('testApp',[]);
app.controller('testCtrl',function($scope){
  $scope.boat ={};
  $scope.boat.description ="The length of the returned array or string. If the limit number is positive, limit number of items from the beginning of the source array/string are copied. If the number is negative, limit number of items from the end of the source array/string are copied. The limit will be trimmed if it exceeds array.length. If limit is undefined, the input will be returned unchanged.";
});
</script>