내가 처음 20 개 단어처럼 특정 단어 번호로이 설명을 다듬을 내 템플릿트림 문자열
<p>{{data.description}}</p>
에 대한 설명을 계산합니다. 많은 필터를 보았지만 특정 문자로 잘립니다. 이로 인해 대부분의 경우 마지막 단어가 중단됩니다.
내가 처음 20 개 단어처럼 특정 단어 번호로이 설명을 다듬을 내 템플릿트림 문자열
<p>{{data.description}}</p>
에 대한 설명을 계산합니다. 많은 필터를 보았지만 특정 문자로 잘립니다. 이로 인해 대부분의 경우 마지막 단어가 중단됩니다.
당신은 공간을 사용하여 단어로 설명 문자열을 분할해야, 다음을 수 :
app.filter('words', function() {
return function (input, words) {
if (isNaN(words)) {
return input;
}
if (words <= 0) {
return '';
}
if (input) {
var inputWords = input.split(/\s+/);
if (inputWords.length > words) {
input = inputWords.slice(0, words).join(' ') + '\u2026';
}
}
return input;
};
});
먼저 내가 설명은 더 이상이라면 내가 확인하고있어, 매개 변수가 숫자인지 확인 우리가 무엇을 다듬을 지 무엇보다, 그리고 나는 나머지를 다듬습니다. 와보기 :
<span>{{((longStringArray = longString.split(' ')) | limitTo: wordLimit = 20).join(' ')}}</span>
<span ng-if="longStringArray.length > wordLimit"> ...</span>
이도 표시 '...'끝이, 지정된 캐릭터 라인이 단축 된 것을 나타냅니다 : 깨끗한 솔루션은이 같은 것
{{data.description | words:250}}
.
//Manish Bhardwaj
var regex = /\s+/gi;
//for count the words.
var count = (data.description).trim().replace(regex,' ').split(' ').length;
// for split all string data in array form
$scope.substr = (data.description).trim().replace(regex,' ').split(' ');
//make substring with space which is trimmed.
$scope.substr1 = $scope.substr.slice(0,20).join(' ');
$scope.substr2 = $scope.substr.slice(20,40).join(' ');
$scope.substr3 = $scope.substr.slice(40,$scope.substr.length).join(' ');