简体   繁体   English

是否有一个基本的角度指令用于读取更多/更少的文本

[英]Is there a basic angular directive for read more/less Text

I've been looking into making an angular directive that would shorten a paragraph or a div if it has more than a certain number of characters(115 for example). 我一直在研究制作一个角度指令,如果它有超过一定数量的字符(例如115),它会缩短段落或div。

I haven't been able to find anything that will work for me, I've seen the http://dotdotdot.frebsite.nl/ and that has worked for some people but I am trying to make it using an angular directive and not JQuery. 我找不到任何对我有用的东西,我已经看过http://dotdotdot.frebsite.nl/ ,这对一些人有用 ,但我试图使用角度指令而不是JQuery的。

If there's any help that someone can offer, it would be greatly appreciated, i am essentially tapped out of ideas. 如果有人可以提供任何帮助,我将非常感激,我基本上是从想法中挖掘出来的。

The is the way my view is setup: 这是我的视图设置方式:

<div class="Description"
<div class="Content">
    <div data-ng-bind-html="item.Description"></div>
</div>

I had originally made it work by just having it as jquery like so but it wasn't advisable to just jquery and angular 我最初只是把它作为jquery就可以了,但是jquery和angular是不可取的

$(function () {

var maxHeight = 115;
var moretext = Localization.Shared.InstitutionProfileStrings.ShowMore();
var lesstext = Localization.Shared.InstitutionProfileStrings.ShowLess();
$('.__Description').each(function () {
    var content = $(this).find(".__Content");
    var anchor = $(this).find(".morelink");

    if ($(content).height() > maxHeight) {
        $(this).addClass("less");
        $(anchor).html(moretext);
        $(anchor).show();
    }
    else {
        $(anchor).hide();
    }
});
$(".morelink").click(function (e) {
    e.preventDefault();
    var parent = this.parentElement;
    if ($(parent).hasClass("less")) {
        $(parent).removeClass("less");
        $(this).html(lesstext);
        $(parent).addClass("more");
    }
    else if ($(parent).hasClass("more")) {
        $(parent).removeClass("more");
        $(this).html(moretext);
        $(parent).addClass("less");
    }
    return false;
 });
});

A quick google showed this package which would seem to fill your need for specific character limit truncation. 一个快速的谷歌显示这个包似乎满足你的特定字符限制截断的需要。

https://github.com/lorenooliveira/ng-text-truncate https://github.com/lorenooliveira/ng-text-truncate

NOTE: I did not write/use that directive so I can't speak to it working properly. 注意:我没有编写/使用该指令,所以我无法正常工作。

如果你想在某一点简单地剪掉文本(但实际上没有改变字符串的值),你可以使用limitTo过滤器:

{{ fullString | limitTo: 115 }}

I think what you're looking for is ng-class . 我认为你要找的是ng-class You can use it to add and remove classes based on a Boolean expression, which is basically what you are doing with your jQuery implementation. 您可以使用它来添加和删除基于布尔表达式的类,这基本上就是您在使用jQuery实现时所做的。 For example: 例如:

HTML: HTML:

<div ng-app="testapp" ng-controller="testctrl">
  <div class="content" ng-class="{ less: hidden }">
  Now is the time for all good men to come to the aid of the party.
  Now is the time for all good men to come to the aid of the party.
  Now is the time for all good men to come to the aid of the party.
  Now is the time for all good men to come to the aid of the party.
  </div>
  <button ng-click="hidden = !hidden">{{hidden ? 'Show' : 'Hide'}}</button>
</div>

JS: JS:

var app = angular.module('testapp', []);
app.controller('testctrl', function ($scope) {
    $scope.hidden = true;
});

You can use a combination of ng-click and interpolation to modify the more/less link. 您可以使用ng-click和插值的组合来修改更多/更少的链接。

Here is a fiddle that shows it working: https://jsfiddle.net/8xjxaz28/ 这是一个显示它工作的小提琴: https//jsfiddle.net/8xjxaz28/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM