如果段落高度达到200px,我想显示“阅读更多”链接。使用Angular的优雅方式是什么?
我的元素 -
<section class="mynotes" ng-if="MyController.mynotes">
<p ng-bind="MyController.mynotes" ng-class="{'showMore': more, 'showLess': !more}"></p>
<section ng-click="MyOtherController.togglearrow($event)">
<i class="down-arrow"></i>
<span ng-click="more = !more">{{more ? 'Less' : 'More'}}</span>
</section>
我想根据p高度隐藏箭头和Less / More文本。有什么想法吗?
答案 0 :(得分:4)
这是一个简单且可配置的指令,我刚刚提出了show less features。
.directive('moreless', function($timeout){
//Default options, default height of 200
var defOption = {
height:200
};
return {
restrict:'EA',
transclude:true, //transclusion enabled
scope:{}, //scoped to isolate since this adds functions on scope
template:'<div class="showless"><div ng-transclude class="content" ng-style="style"></div><section class="trigger" ng-if="showSection"> \
<i class="down-arrow"></i> \
<span ng-click="toggleMore()">{{{true:"More", false:"Less"}[more]}}</span> \
</section></div>',
link:function(scope, elm, attrs){
var origHeight,
//Construct the options from default options and override by any thing specified
options = angular.extend({}, angular.copy(defOption), scope.$eval(attrs.options));
//Initialize
initialize();
function initialize(){
scope.toggleMore = toggleMore;
scope.toggleSection = toggleSection;
//Do not show showless section initially
scope.toggleSection(false);
//Wait for one digest cycle to complete and intialize showless componenet
$timeout(_initShowLess);
}
function _initShowLess(){
//Set initial height and get the height
origHeight = elm.find('.content').height();
scope.style = {height:origHeight};
//compare the height to allowed height and togglesection and set showmore accordingly
if(origHeight > options.height){
scope.toggleSection(true);
scope.style.height = options.height;
scope.toggleMore();
}
}
function toggleMore(){
expandCollapse(scope.more);
scope.more = !scope.more;
}
function toggleSection(show){
scope.showSection = show;
}
function expandCollapse(expand){
var height = expand ? origHeight : options.height;
scope.style.height = height;
}
}
}
});
默认设置设置为200高度,您可以通过options属性进行配置。因此用法类似于:
<moreless options="{height:300}">
<p ng-bind="mynotes2"></p>
</moreless>
<强> Demo 强>
内联演示&#39;
var app = angular.module('plunker', []);
app.directive('moreless', function($timeout) {
var defOption = {
height: 200
};
var defOffset = 10;
return {
restrict: 'EA',
transclude: true,
scope: {},
template: '<div class="showless"><div ng-transclude class="content" ng-style="style"></div><section class="trigger" ng-if="showSection"> \
<i class="down-arrow"></i> \
<span ng-click="toggleMore()">{{{true:"More", false:"Less"}[more]}}</span> \
</section></div>',
link: function(scope, elm, attrs) {
var origHeight,
options = angular.extend({}, angular.copy(defOption), scope.$eval(attrs.options));
initialize();
function initialize() {
scope.toggleMore = toggleMore;
scope.toggleSection = toggleSection;
scope.toggleSection(false);
$timeout(_initShowLess);
}
function _initShowLess() {
origHeight = elm.find('.content').height();
scope.style = {
height: origHeight
};
if (origHeight > options.height) {
scope.toggleSection(true);
scope.style.height = options.height;
scope.toggleMore();
}
}
function toggleMore() {
expandCollapse(scope.more);
scope.more = !scope.more;
}
function toggleSection(show) {
scope.showSection = show;
}
function expandCollapse(expand) {
var height = expand ? origHeight : options.height;
scope.style.height = height;
}
}
}
}).controller('MainCtrl', function($scope) {
$scope.mynotes = 'Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.(The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.';
$scope.mynotes2 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
})
&#13;
/* Put your css in here */
.showless {
position: relative;
}
.showless .content {
overflow: hidden;
transition: all linear 0.5s;
-webkit-transition: all linear 0.5s;
}
.showless .trigger {
background: #cdcdcd;
display: inline-block;
}
&#13;
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script data-require="jquery@*" data-semver="2.1.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link data-require="bootstrap-css@*" data-semver="3.3.1" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.10/angular.js" data-semver="1.3.10"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<div>
<moreless>
<p ng-bind="mynotes"></p>
</moreless>
</div>
<div>
<moreless options="{height:100}">
<p ng-bind="mynotes"></p>
</moreless>
</div>
<div>
<moreless options="{height:300}">
<p ng-bind="mynotes2"></p>
</moreless>
</div>
</body>
</html>
&#13;
答案 1 :(得分:2)
我已经从头脑中编写了以下指令,但你应该能够理解。
angular..directive('setMore',function(){
return {
restrict: 'A',
link: function($scope,$el,$attr) {
$scope.$watch(function() {
return $el.height();
},function(height) {
$scope.more = height > ~~$attr['setMore'];
}
});
}
};
});
你可以这样使用它。
<p set-more="100" ng-bind="MyController.mynotes" ng-class="{'showMore': more, 'showLess': !more}"></p>