如果条件满足Angular如何添加CSSclassJS
How can add a CSS class if a condition is met Angular JS
我正在创建一个截断指令,如果字符超过 10 个,我将截断文本字符串。然后它将显示“...”。
我的目标是编写一个条件,如果字符少于 10 个,则删除“...”。有点坚持这一点,如果有人对我如何实现这个有任何想法,欢迎提出建议。
这是我的代码:
var app = angular.module('myApp', []);
// Controller
app.controller('mainCtrl', function($scope) {
$scope.text = "John Doe Blah blah";
});
// Directive
app.directive('truncate', function() {
function link(scope, element, attrs){
scope.truncate = function(str){
if(str.length > 10) {
return 'truncate'
} else{
return 'notruncate'
}
}
}
// Write a condition to check if the username is < 10 characters to hide the ellipse
return{
restrict: 'A',
scope: {
input: '=',
maxCharacters: '=',
href: '=',
isShowMore: '='
},
template: '<h4 ng-init="limit=true;length=maxCharacters">{{input | limitTo: length}}<a ng-attr-href="#" ng-click="limit=!limit;length=limit?maxCharacters: \'\'">{{isShowMore?"Show More":"..."}}</a></h4>',
link: link
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<html ng-app='myApp'>
<body ng-controller='mainCtrl'>
<div href='true' input='text' is-show-more='false' max-characters='10' truncate=''></div>
</body>
</html>
Angular 有一个 ngClass 指令,它将根据表达式的计算文本应用 class。只需编写一个 returns 不同 class 的函数,具体取决于字符串长度,然后在 ngClass 中调用它。
ngClass 的文档:https://docs.angularjs.org/api/ng/directive/ngClass
示例代码片段
var app = angular.module('myApp', []);
// Controller
app.controller('mainCtrl', function($scope) {
$scope.text = "John Doe Blah blah";
$scope.truncate = function(str){
if (str.length > 10) {
return 'truncate'
} else {
return 'notruncate'
}
}
});
.truncate {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.notruncate {
white-space: nowrap;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html ng-app="myApp">
<div ng-controller="mainCtrl" ng-class="truncate(text)" style="width: 40px">{{text}}</div>
</html>
您可以简单地在包含“...”的 span
元素上使用 ngHide
指令,条件如下:
ng-hide="input.length <= maxCharacters || !length"
这意味着如果输入的长度小于或等于 maxCharacters
或未应用过滤器,则此元素将被隐藏。
基于您的代码笔的工作示例:
var app = angular.module('myApp', []);
// Controller
app.controller('mainCtrl', function($scope) {
$scope.text = "John Doe Blah blah";
});
// Directive
app.directive('truncate', function() {
// Write a condition to check if the username is < 10 characters to hide the ellipse
return {
restrict: 'A',
scope: {
input: '=',
maxCharacters: '=',
href: '=',
isShowMore: '='
},
template: '<h4 ng-init="limit=true;length=maxCharacters">{{input | limitTo: length}}<a ng-attr-href="#" ng-click="limit=!limit;length=limit?maxCharacters: \'\'" ng-hide="input.length <= maxCharacters || !length" >{{isShowMore?"Show More":"..."}}</a></h4>'
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<html ng-app='myApp'>
<body ng-controller='mainCtrl'>
<div href='true' input='text' is-show-more='false' max-characters='10' truncate=''></div>
</body>
</html>
你可以尝试这样的事情。
"use strict";
function exampleController($scope) {
$scope.example = "Yeyuh im so long, where does it end?!";
}
function truncate() {
return {
restrict: "A",
scope: {
text: "=",
length: "="
},
link: function($scope, $element, $attrs) {
var elm = $element[0];
$scope.$watch("text", function(newText, oldText) {
if (elm.classList.value.indexOf("notruncate") > -1) {
elm.classList.remove("notruncate");
}
if (newText.length > $scope.length) {
elm.className = "truncate";
}
if (newText.length < $scope.length) {
if (elm.classList.value.indexOf("truncate") > -1) {
elm.classList.remove("truncate");
}
elm.className = "notruncate";
}
});
}
};
}
angular
.module("example", [])
.controller("exampleController", exampleController)
.directive("truncate", truncate);
.truncate {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.notruncate {
white-space: nowrap;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="container-fluid" ng-app="example">
<div class="container" ng-controller="exampleController">
<input type="text" ng-model="example" truncate text="example" length="10">
</div>
</div>
我正在创建一个截断指令,如果字符超过 10 个,我将截断文本字符串。然后它将显示“...”。
我的目标是编写一个条件,如果字符少于 10 个,则删除“...”。有点坚持这一点,如果有人对我如何实现这个有任何想法,欢迎提出建议。
这是我的代码:
var app = angular.module('myApp', []);
// Controller
app.controller('mainCtrl', function($scope) {
$scope.text = "John Doe Blah blah";
});
// Directive
app.directive('truncate', function() {
function link(scope, element, attrs){
scope.truncate = function(str){
if(str.length > 10) {
return 'truncate'
} else{
return 'notruncate'
}
}
}
// Write a condition to check if the username is < 10 characters to hide the ellipse
return{
restrict: 'A',
scope: {
input: '=',
maxCharacters: '=',
href: '=',
isShowMore: '='
},
template: '<h4 ng-init="limit=true;length=maxCharacters">{{input | limitTo: length}}<a ng-attr-href="#" ng-click="limit=!limit;length=limit?maxCharacters: \'\'">{{isShowMore?"Show More":"..."}}</a></h4>',
link: link
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<html ng-app='myApp'>
<body ng-controller='mainCtrl'>
<div href='true' input='text' is-show-more='false' max-characters='10' truncate=''></div>
</body>
</html>
Angular 有一个 ngClass 指令,它将根据表达式的计算文本应用 class。只需编写一个 returns 不同 class 的函数,具体取决于字符串长度,然后在 ngClass 中调用它。
ngClass 的文档:https://docs.angularjs.org/api/ng/directive/ngClass
示例代码片段
var app = angular.module('myApp', []);
// Controller
app.controller('mainCtrl', function($scope) {
$scope.text = "John Doe Blah blah";
$scope.truncate = function(str){
if (str.length > 10) {
return 'truncate'
} else {
return 'notruncate'
}
}
});
.truncate {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.notruncate {
white-space: nowrap;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html ng-app="myApp">
<div ng-controller="mainCtrl" ng-class="truncate(text)" style="width: 40px">{{text}}</div>
</html>
您可以简单地在包含“...”的 span
元素上使用 ngHide
指令,条件如下:
ng-hide="input.length <= maxCharacters || !length"
这意味着如果输入的长度小于或等于 maxCharacters
或未应用过滤器,则此元素将被隐藏。
基于您的代码笔的工作示例:
var app = angular.module('myApp', []);
// Controller
app.controller('mainCtrl', function($scope) {
$scope.text = "John Doe Blah blah";
});
// Directive
app.directive('truncate', function() {
// Write a condition to check if the username is < 10 characters to hide the ellipse
return {
restrict: 'A',
scope: {
input: '=',
maxCharacters: '=',
href: '=',
isShowMore: '='
},
template: '<h4 ng-init="limit=true;length=maxCharacters">{{input | limitTo: length}}<a ng-attr-href="#" ng-click="limit=!limit;length=limit?maxCharacters: \'\'" ng-hide="input.length <= maxCharacters || !length" >{{isShowMore?"Show More":"..."}}</a></h4>'
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<html ng-app='myApp'>
<body ng-controller='mainCtrl'>
<div href='true' input='text' is-show-more='false' max-characters='10' truncate=''></div>
</body>
</html>
你可以尝试这样的事情。
"use strict";
function exampleController($scope) {
$scope.example = "Yeyuh im so long, where does it end?!";
}
function truncate() {
return {
restrict: "A",
scope: {
text: "=",
length: "="
},
link: function($scope, $element, $attrs) {
var elm = $element[0];
$scope.$watch("text", function(newText, oldText) {
if (elm.classList.value.indexOf("notruncate") > -1) {
elm.classList.remove("notruncate");
}
if (newText.length > $scope.length) {
elm.className = "truncate";
}
if (newText.length < $scope.length) {
if (elm.classList.value.indexOf("truncate") > -1) {
elm.classList.remove("truncate");
}
elm.className = "notruncate";
}
});
}
};
}
angular
.module("example", [])
.controller("exampleController", exampleController)
.directive("truncate", truncate);
.truncate {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.notruncate {
white-space: nowrap;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="container-fluid" ng-app="example">
<div class="container" ng-controller="exampleController">
<input type="text" ng-model="example" truncate text="example" length="10">
</div>
</div>