gpt4 book ai didi

javascript - 在 AngularJS 中显示更多功能?

转载 作者:行者123 更新时间:2023-12-02 00:37:56 24 4
gpt4 key购买 nike

我有一个项目列表及其信息。问题是我想显示最多 50 的描述人物。如果超过这个值我想显示 show more按钮。单击该按钮后,我想显示全文。我想用过滤器来做到这一点,但我不知道如何实现这一点。

{{jobs.description | limitTo: 2}}{{jobs.description.length>20 ? '...':''}}

我可以写<a href="">show more</a>吗?链接位于字符 ... 的位置?

或者还有其他方法可以实现我的目标吗?

最佳答案

观察:

  • 您的实现是正确的。问题出在您的 AngularJS 版本上。
  • v1.2.1 开始,AngularJS limitTo 过滤器可用于数组和字符串。<

工作演示

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function($scope) {

// Initial 50 characters will be displayed.
$scope.strLimit = 50;

// String
$scope.jobs = {
description: "Hi I have a list of items along with their information. The problem is I want to show the description up to 50 letters, but if it exceeds this value I want to show show more button upon clicking it I want to show the full text. I want to do it with filters, but I don't know one could achieve this with my way."
};

// Event trigger on click of the Show more button.
$scope.showMore = function() {
$scope.strLimit = $scope.jobs.description.length;
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
{{ jobs.description | limitTo:strLimit }}
<span ng-if="jobs.description.length > 50">
<button ng-click="showMore()">Show more</button>
</span>
</div>

根据评论更新了Plnkr,并添加了显示更少功能。

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function($scope) {

// Initial 50 characters will be displayed.
$scope.strLimit = 50;

// String
$scope.jobs = {
description: "Hi. I have a list of items along with their information. The problem is I want to show the description up to 50 letters, but if it exceeds this value I want to show show more button upon clicking it I want to show the full text. I want to do it with filters, but I don't know one could achieve this with my way."
};

// Event trigger on click of the Show more button.
$scope.showMore = function() {
$scope.strLimit = $scope.jobs.description.length;
};

// Event trigger on click on the Show less button.
$scope.showLess = function() {
$scope.strLimit = 50;
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
{{ jobs.description | limitTo:strLimit }}
<span ng-if="jobs.description.length > 50 && jobs.description.length != strLimit">
<button ng-click="showMore()">Show more</button>
</span>
<span ng-if="jobs.description.length == strLimit">
<button ng-click="showLess()">Show less</button>
</span>
</div>

关于javascript - 在 AngularJS 中显示更多功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43283364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com